﻿---@diagnostic disable: trailing-space
local extension = Package:new("moepack_moesp2")
extension.extensionName = "moepack"

Fk:loadTranslationTable{
  ["moepack_moesp2"] = "萌包-SP2",
  ["moesp2"] = "萌SP2",
}

local U = require "packages/utility/utility"

local MoeFunc = require "packages/moepack/moepack_function"

local emoprincess = General(extension, "moesp2__emoprincess", "moe", 3, 3, General.Female)
emoprincess.hidden = true

local emo__kuyi = fk.CreateViewAsSkill{
  name = "emo__kuyi",
  anim_type = "support",
  pattern = "slash,jink",
  prompt = "#emo__kuyi",
  interaction = function(self)
    local names = U.getViewAsCardNames(Self, self.name, {"slash", "jink"})
    if #names > 0 then
      return U.CardNameBox { choices = names }
    end
  end,
  card_filter = function(self, to_select, selected)
    return #selected < 2
  end,
  view_as = function(self, cards)
    if #cards ~= 2 or not self.interaction.data then return nil end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
  before_use = function(self, player, use)
    -- 防一手许劭秒丢技能和孙鲁班修改使用者
    -- 你妹的，打出加不上extra_data
    player.room:setCardMark(use.card, "emo__kuyiOwner", player.id)
  end,
  enabled_at_play = function(self, player)
    return true
  end,
  enabled_at_response = function (self, player, response)
    return true
  end,
}

local emo__kuyi_delay = fk.CreateTriggerSkill{
  name = "#emo__kuyi_delay",
  mute = true,
  events = {fk.CardUseFinished, fk.CardRespondFinished, fk.RoundEnd},
  can_trigger = function(self, event, target, player, data)
    if event ~= fk.RoundEnd then
      return not player.dead and data.card:getMark("emo__kuyiOwner") == player.id and #data.card.subcards == 2
    else
      return player:getMark("emo__kuyi_skill-round") ~= 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event ~= fk.RoundEnd then
      local types = {"basic", "trick", "equip"}
      for _, id in ipairs(data.card.subcards) do
        table.removeOne(types, Fk:getCardById(id):getTypeString())
      end
      local cardType = (#types == 1) and types[1] or Fk:getCardById(data.card.subcards[1]):getTypeString()
      local ids = room:getCardsFromPileByRule(".|.|.|.|.|"..cardType, 1, "allPiles")
      if #ids > 0 then
        room:moveCardTo(ids, Player.Hand, player, fk.ReasonJustMove, self.name, nil, true, player.id)
        if not player.dead and player:getMark("emo__kuyi_gain-turn") == 0 then
          room:setPlayerMark(player, "emo__kuyi_gain-turn", 1)
          local list = {["basic"] = "qinzheng", ["trick"] = "chengqi", ["equip"] = "yingtu"}
          local skill = list[cardType]
          if Fk.skills[skill] and not player:hasSkill(skill, true) then
            room:addTableMark(player, "emo__kuyi_skill-round", skill)
            room:handleAddLoseSkills(player, skill)
          end
        end
      end
    else
      room:handleAddLoseSkills(player, "-"..table.concat(player:getTableMark("emo__kuyi_skill-round"), "|-"))
    end
  end,
}
emo__kuyi:addRelatedSkill(emo__kuyi_delay)

local emo__kuyi_prohibit = fk.CreateProhibitSkill{
  name = "#emo__kuyi_prohibit",
  is_prohibited = function(self, from, to, card)
    return to == from and card and table.contains(card.skillNames, "emo__kuyi")
  end,
}
emo__kuyi:addRelatedSkill(emo__kuyi_prohibit)

emoprincess:addSkill(emo__kuyi)

local emo__qimo = fk.CreateTriggerSkill{
  name = "emo__qimo",
  events = {fk.CardEffectFinished},
  frequency = Skill.Compulsory,
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and data.card.type == Card.TypeTrick
    and not (data.card:isVirtual() and #data.card.subcards == 0) and #player:getTableMark("emo__qimo_record") < 3
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(Card:getIdList(data.card), function (id)
      return room:getCardArea(id) == Card.Processing
    end)
    if #cards > 0 then
      room:obtainCard(player, cards, true, fk.ReasonJustMove, player.id, self.name)
    end
    if not player.dead then
      local choices = table.filter({"#emo__qimo_wuyan", "#emo__qimo_delete", "#emo__qimo_change"}, function (ch)
        return not table.contains(player:getTableMark("emo__qimo_record"), ch)
      end)
      local skills = table.map(table.filter(player.player_skills, function (s)
        return s:isPlayerSkill(player) and s.visible
      end), Util.NameMapper)
      table.removeOne(skills, self.name)
      if #skills == 0 then
        table.removeOne(choices, "#emo__qimo_delete")
      end
      if player:hasSkill("wuyan", true) then
        table.removeOne(choices, "#emo__qimo_wuyan")
      end
      if #choices == 0 then return end
      local choice = room:askForChoice(player, choices, self.name)
      room:addTableMark(player, "emo__qimo_record", choice)
      if choice == "#emo__qimo_wuyan" then
        room:handleAddLoseSkills(player, "wuyan")
      elseif choice == "#emo__qimo_delete" then
        choice = room:askForChoice(player, skills, self.name, "#emo__qimo-delete", true)
        room:handleAddLoseSkills(player, "-"..choice)
      else
        local deputy = player.deputyGeneral == "moesp2__emoprincess"
        local newGeneral = "mq__qi"
        if Fk.generals[newGeneral] == nil then newGeneral = "moesp__qi" end
        room:changeHero(player, newGeneral, false, deputy, true, true, true)
      end
    end
  end,

  refresh_events = {fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    return target == player and data == self
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "emo__qimo_record", 0)
  end,
}

emoprincess:addSkill(emo__qimo)

emoprincess:addRelatedSkill("qinzheng")
emoprincess:addRelatedSkill("chengqi")
emoprincess:addRelatedSkill("yingtu")
emoprincess:addRelatedSkill("wuyan")

Fk:loadTranslationTable{
  ["moesp2__emoprincess"] = "伊默",
  ["#moesp2__emoprincess"] = "",
  
  ["emo__kuyi"] = "苦肄",
  [":emo__kuyi"] = "你可以将两张牌当一张【杀】或【闪】使用或打出，结算后若这两张牌类型相同，你获得一张同类型牌，若类型不同，你获得一张类型与两者均不同的牌。你根据每回合首次因此技能获得的牌的类型于本轮获得技能：基本〖勤政〗；锦囊〖承启〗；装备〖营图〗。",
  ["#emo__kuyi"] = "苦肄：将两张牌当【杀】或【闪】使用或打出",
  ["#emo__kuyi_delay"] = "苦肄",

  ["emo__qimo"] = "祈默",
  [":emo__qimo"] = "锁定技，每当非虚拟锦囊牌对你生效后，你获得此牌，执行并删除一项：1.获得技能〖无言〗；2.失去一个其他技能；3.变更武将为“祈”。",
  ["#emo__qimo_wuyan"] = "获得技能〖无言〗",
  ["#emo__qimo_delete"] = "失去一个其他技能",
  ["#emo__qimo_change"] = "变更武将为“祈”",
  ["#emo__qimo-delete"] = "祈默：选择一个技能删去",
}


local muyaomeng = General(extension, "moesp__nomad_youmu", "moe", 4, 4, General.Female)

local emo__mujie = fk.CreateTriggerSkill{
  name = "emo__mujie",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player:getHandcardNum() < player.hp then
      for _, move in ipairs(data) do
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(player.hp - player:getHandcardNum(), self.name)
  end,
}
muyaomeng:addSkill(emo__mujie)

local emo__mingjian = fk.CreateTriggerSkill{
  name = "emo__mingjian",
  anim_type = "offensive",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.is_damage_card and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local type = data.card:getTypeString()
    local ids = table.filter(player:getCardIds("he"), function (id)
      local c = Fk:getCardById(id)
      return not player:prohibitDiscard(c) and not c.is_damage_card
    end)
    local card = player.room:askForDiscard(player, 1, 1, true, self.name, true, tostring(Exppattern{ id = ids }),
      "#emo__mingjian-invoke:::"..data.card:toLogString(), true)
    if #card > 0 then
      self.cost_data = card
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:throwCard(self.cost_data, self.name, player, player)
    data.additionalEffect = (data.additionalEffect or 0) + 1
  end,
}
muyaomeng:addSkill(emo__mingjian)

Fk:loadTranslationTable{
  ["moesp__nomad_youmu"] = "牧妖梦",
  ["#moesp__nomad_youmu"] = "幽人的牧歌",
  ["designer:moesp__nomad_youmu"] = "妖梦厨",
  ["illustrator:moesp__nomad_youmu"] = "AI",
  
  ["emo__mujie"] = "牧节",
  [":emo__mujie"] = "锁定技，当你失去手牌后，你将手牌摸至体力值。",

  ["emo__mingjian"] = "暝剑",
  [":emo__mingjian"] = "当你使用伤害牌时，你可以弃置一张非伤害牌，令此牌额外结算一次。",
  ["#emo__mingjian-invoke"] = "暝剑：你可以弃置一张非伤害牌，令%arg额外结算一次",
}

local charlotte = General(extension, "moesp__charlotte_skopovskaya", "moe", 4, 4, General.Female)

local emo__xingcun = fk.CreateTriggerSkill{
  name = "emo__xingcun",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.Deathed},
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:isWounded() then
      room:recover { num = player.maxHp - player.hp, skillName = self.name, who = player, recoverBy = player }
    else
      player:drawCards(3, self.name)
    end
  end,
}
charlotte:addSkill(emo__xingcun)

local emo__jueyi = fk.CreateActiveSkill{
  name = "emo__jueyi",
  anim_type = "offensive",
  card_num = 0,
  target_num = 0,
  card_filter = Util.FalseFunc,
  interaction = function()
    local all_choices = {"#emo__jueyi-loseHp", "#emo__jueyi-discard"}
    local choices = {}
    if Self.hp > 1 then
      table.insert(choices, all_choices[1])
    end
    if not Self:isKongcheng() then
      table.insert(choices, all_choices[2])
    end
    return UI.ComboBox { choices = choices ,all_choices = all_choices }
  end,
  prompt = "#emo__jueyi-prompt",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and (player.hp > 1 or not player:isKongcheng())
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local choice = self.interaction.data
    local num = 0
    if choice == "#emo__jueyi-loseHp" then
      num = player.hp - 1
      room:loseHp(player, num, self.name)
    else
      num = player:getHandcardNum()
      player:throwAllCards("h")
    end
    if player.dead then return end
    room:setPlayerMark(player, "emo__jueyi-turn", 1)
    room:addPlayerMark(player, MarkEnum.AddMaxCardsInTurn, num)
    player:drawCards(num, self.name, "top", "@@emo__jueyi-turn-inhand")
  end,
}

local emo__jueyi_delay = fk.CreateTriggerSkill{
  name = "#emo__jueyi_delay",
  events = {fk.TurnEnd},
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("emo__jueyi-turn") > 0 and player:isWounded()
    and not table.find(player:getCardIds("h"), function (id)
      return Fk:getCardById(id):getMark("@@emo__jueyi-turn-inhand") ~= 0
    end)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:recover { num = 1, skillName = self.name, who = player, recoverBy = player}
  end,
}
emo__jueyi:addRelatedSkill(emo__jueyi_delay)

charlotte:addSkill(emo__jueyi)

local emo__xinji = fk.CreateTriggerSkill{
  name = "emo__xinji",
  frequency = Skill.Wake,
  events = {fk.EventPhaseStart},
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
    and player:hasSkill(self) and target == player and player.phase == Player.Start
  end,
  can_wake = function(self, event, target, player, data)
    return player:getHandcardNum() < 2 or player.hp < 2
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, self.name)
    if player.dead then return end
    room:changeMaxHp(player, 1)
    if player.dead then return end
    room:recover { num = 1, skillName = self.name, who = player, recoverBy = player }
    room:handleAddLoseSkills(player, "emo__jiaoliu")
  end,
}
charlotte:addSkill(emo__xinji)

local emo__jiaoliu = fk.CreateActiveSkill{
  name = "emo__jiaoliu",
  anim_type = "support",
  card_num = 1,
  target_num = 1,
  prompt = "#emo__jiaoliu-prompt",
  card_filter = function (self, to_select, selected)
    return #selected == 0 and table.contains(Self.player_cards[Player.Hand], to_select)
  end,
  target_filter = function (self, to_select, selected, selected_cards)
    return #selected == 0 and Self.id ~= to_select and #selected_cards == 1
    and not Fk:currentRoom():getPlayerById(to_select):isKongcheng()
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    local ur_cards = to:getCardIds("h")
    if #ur_cards > 1 then
      ur_cards = room:askForCard(to, 1, 1, false, self.name, false, ".", "#emo__jiaoliu-card:"..player.id)
    end
    local get = ur_cards[1]
    local draw = not Fk:getCardById(get).is_damage_card
    U.swapCards(room, player, player, to, effect.cards, ur_cards, self.name, Player.Hand)
    if table.contains(player.player_cards[Player.Hand], get) then
      room:setCardMark(Fk:getCardById(get), "@@emo__jiaoliu-inhand", 1)
    end
    if draw then
      if not player.dead then
        player:drawCards(1, self.name)
      end
      if not to.dead then
        to:drawCards(1, self.name)
      end
    end 
  end,
}

local emo__jiaoliu_trigger = fk.CreateTriggerSkill{
  name = "#emo__jiaoliu_trigger",
  anim_type = "support",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(emo__jiaoliu) and target == player and not player:isKongcheng()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function (p) return not p:isKongcheng() end)
    local tos, cardId = room:askForChooseCardAndPlayers(player, table.map(targets, Util.IdMapper), 1, 1
    , ".|.|.|hand", "#emo__jiaoliu-prompt", "emo__jiaoliu", true)
    if #tos > 0 then
      self.cost_data = { tos = tos, cards = {cardId} }
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    emo__jiaoliu:onUse(player.room, { from = player.id, tos = self.cost_data.tos, cards = self.cost_data.cards })
  end,

  refresh_events = {fk.PreCardUse},
  can_refresh = function (self, event, target, player, data)
    return target == player and data.card.is_damage_card
  end,
  on_refresh = function (self, event, target, player, data)
    for _, id in ipairs(Card:getIdList(data.card)) do
      if Fk:getCardById(id).is_damage_card and Fk:getCardById(id):getMark("@@emo__jiaoliu-inhand") ~= 0 then
        data.additionalDamage = (data.additionalDamage or 0) + 1
      end
    end
  end,
}
emo__jiaoliu:addRelatedSkill(emo__jiaoliu_trigger)

local emo__jiaoliu_maxcards = fk.CreateMaxCardsSkill{
  name = "#emo__jiaoliu_maxcards",
  exclude_from = function(self, player, card)
    return card and card:getMark("@@emo__jiaoliu-inhand") > 0
  end,
}
emo__jiaoliu:addRelatedSkill(emo__jiaoliu_maxcards)

charlotte:addRelatedSkill(emo__jiaoliu)


Fk:loadTranslationTable{
  ["moesp__charlotte_skopovskaya"] = "夏洛特", -- 夏洛特•斯可波夫斯加
  ["#moesp__charlotte_skopovskaya"] = "战争的幸存者",
  ["designer:moesp__charlotte_skopovskaya"] = "晴天娃娃",
  
  ["emo__xingcun"] = "幸存",
  [":emo__xingcun"] = "锁定技，当其他角色死亡后，你将回复体力至体力上限，若你未受伤则改为摸三张牌。",

  ["emo__xinji"] = "心悸",
  [":emo__xinji"] = "觉醒技，准备阶段，若你的体力值或手牌数小于2，则你摸两张牌，增加1点体力上限并回复1点体力，然后获得技能〖交流〗。",

  ["emo__jueyi"] = "决意",
  [":emo__jueyi"] = "出牌阶段限一次，你可以失去体力至1点或弃置所有手牌，若如此做，你摸X张牌且本回合手牌上限+X，且本回合结束时，若这些牌均不在你手牌中，你回复一点体力（X为失去体力数或弃牌数）。",
  ["@@emo__jueyi-turn-inhand"] = "决意",
  ["#emo__jueyi_delay"] = "决意",
  ["#emo__jueyi-prompt"] = "决意：你可失去体力至1点或弃置所有手牌，摸失去体力数的两倍或弃牌数张牌",
  ["#emo__jueyi-loseHp"] = "失去体力至1点",
  ["#emo__jueyi-discard"] = "弃置所有手牌",

  ["emo__jiaoliu"] = "交流",
  [":emo__jiaoliu"] = "出牌阶段限一次，或你受到伤害后，你可以与一名其他角色同时交换一张手牌，你以此法获得的牌不计入手牌上限，且若此牌为：伤害牌，此牌伤害基数+1；非伤害牌，你与其各摸一张牌。",
  ["#emo__jiaoliu-prompt"] = "交流：选择一张手牌和一名其他角色，用此牌交换其一张手牌",
  ["#emo__jiaoliu-card"] = "交流：请选择一张手牌，与 %src 交换",
  ["@@emo__jiaoliu-inhand"] = "交流",
  ["#emo__jiaoliu_trigger"] = "交流",

  ["$emo__xinji1"] = "",
  ["$emo__xinji2"] = "",
}

local moesp__hkrifle = General(extension, "moesp__hkrifle", "moe", 4, 4, General.Female)

local emo__jingying = fk.CreateTriggerSkill{
  name = "emo__jingying",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseChanging, fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == fk.EventPhaseChanging then 
        return data.to == Player.Discard and #player.room.logic:getActualDamageEvents(1, function(e) 
          return e.data[1].from == player or e.data[1].to == player 
        end, Player.HistoryRound) > 0
      elseif event == fk.CardUseFinished then 
        return player.phase == Player.Play and data.card.trueName == "slash" and player:getMark("emo__jingying_draw-phase") == 0
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    if event == fk.EventPhaseChanging then 
      room:notifySkillInvoked(player, self.name, "offensive")
      data.to = Player.Play
      room:sendLog{
        type = "#PhaseChanged",
        from = player.id,
        arg = "phase_discard",
        arg2 = "phase_play",
      }
    else
      room:notifySkillInvoked(player, self.name, "drawcard")
      player:endPlayPhase()
      -- 防止多次触发
      room:setPlayerMark(player, "emo__jingying_draw-phase", 1)
      local types = {}
      room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data[1]
        if use.from == player.id then
          table.insertIfNeed(types, use.card.type)
        end
        return #types == 3
      end, Player.HistoryPhase)
      player:drawCards(#types, self.name)
      -- this log should be deleted
      room:sendLog{ type = "#emo__jingying_endplay", from = player.id, arg = self.name }
      -- 疑似会出现客户端无法清空卡牌使用次数的BUG，不知如何触发
      --room:delay(2000)
    end
  end,
}
moesp__hkrifle:addSkill(emo__jingying)

local emo__yazhi = fk.CreateViewAsSkill{
  name = "emo__yazhi",
  anim_type = "offensive",
  pattern = "slash",
  prompt = "#emo__yazhi-prompt",
  card_filter = Util.FalseFunc,
  view_as = function(self)
    local cards = Self:getCardIds("h")
    if #cards > 0 then
      local name = "slash"
      if #cards > 1 and table.every(cards, function (id) return Fk:getCardById(id).color == Card.Red end) then
        name = "fire__slash"
      end
      local c = Fk:cloneCard(name)
      c.skillName = self.name
      c:addSubcards(cards)
      return c
    end
  end,
  before_use = function(self, player, use)
    local room = player.room
    if #use.card.subcards > 2 then
      player:drawCards(1, self.name)
    end
    if #use.card.subcards > 4 then
      use.disresponsiveList = table.map(room.alive_players, Util.IdMapper)
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  enabled_at_response = Util.FalseFunc,
}

local emo__yazhi_targetmod = fk.CreateTargetModSkill{
  name = "#emo__yazhi_targetmod",
  bypass_distances = function(self, player, skill, card, to)
    return card and table.contains(card.skillNames, emo__yazhi.name)
  end,
  extra_target_func = function(self, player, skill, card)
    if card and table.contains(card.skillNames, emo__yazhi.name) and #card.subcards > 3 then
      return 1
    end
  end,
}
emo__yazhi:addRelatedSkill(emo__yazhi_targetmod)

moesp__hkrifle:addSkill(emo__yazhi)

Fk:loadTranslationTable{
  ["moesp__hkrifle"] = "HK416",
  ["#moesp__hkrifle"] = "蓝色咆哮",
  ["designer:moesp__hkrifle"] = "不明觉厉de",
  
  ["emo__jingying"] = "菁英",
  [":emo__jingying"] = "锁定技，若你本轮受到或造成过伤害，你的弃牌阶段改为出牌阶段。你于你出牌阶段内使用的【杀】结算结束后，你摸X张牌，然后结束当前阶段（X为你于本阶段使用牌的类型数）。",
  ["#emo__jingying_endplay"] = "由于 %arg 的效果，%from 结束出牌阶段",

  ["emo__yazhi"] = "压制",
  [":emo__yazhi"] = "出牌阶段限一次，你可以将所有手牌当做无距离限制的【杀】使用，根据以此法使用牌获得效果：大于1张且均为红色，改为火【杀】；大于2张，摸一张牌；大于3张，目标上限加一；大于4张，不可被响应。",
  ["#emo__yazhi-prompt"] = "压制：将所有手牌当无距离限制的【杀】使用！",
}

local moesp__caveira = General(extension, "moesp__caveira", "moe", 2, 2, General.Female)

local emo__sujia = fk.CreateTriggerSkill{
  name = "emo__sujia",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseChanging, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.EventPhaseChanging then 
        return target == player and data.to == Player.Draw
      elseif event == fk.EventPhaseStart then 
        return target == player and player.phase == Player.Start
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    if event == fk.EventPhaseChanging then 
      room:notifySkillInvoked(player, self.name, "negative")
      player:skip(data.to)
      return true
    else
      room:notifySkillInvoked(player, self.name, "drawcard")
      player:drawCards(3, self.name)
    end
  end,
}
moesp__caveira:addSkill(emo__sujia)

local emo__jingbu = fk.CreateTriggerSkill{
  name = "emo__jingbu",
  frequency = Skill.Compulsory,
  anim_type = "offensive",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target and player.phase ~= Player.NotActive
    and player:getMark("emo__jingbu_me-turn") == 0
  end,
  on_use = function(self, event, target, player, data)
    data.disresponsiveList = table.map(player.room.alive_players, Util.IdMapper)
  end,

  refresh_events = {fk.CardUsing},
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(self, true) and target.phase ~= Player.NotActive then
      if target == player then
        return data.card.type == Card.TypeEquip and player:getMark("emo__jingbu_me-turn") == 0
      else
        return data.card.type ~= Card.TypeEquip and target:getMark("emo__jingbu_other-turn") == 0
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if target == player then
      room:setPlayerMark(player, "emo__jingbu_me-turn", 1)
    else
      room:setPlayerMark(target, "emo__jingbu_other-turn", 1)
    end
  end,
}

local emo__jingbu_targetmod = fk.CreateTargetModSkill{
  name = "#emo__jingbu_targetmod",
  bypass_distances = function (self, player, skill, card)
    return card and player:hasSkill(emo__jingbu) and player.phase ~= Player.NotActive and player:getMark("emo__jingbu_me-turn") == 0
  end,
}
emo__jingbu:addRelatedSkill(emo__jingbu_targetmod)

local emo__jingbu_maxcards = fk.CreateMaxCardsSkill{
  name = "#emo__jingbu_maxcards",
  correct_func = function(self, player)
    if player:hasSkill(emo__jingbu) and player.phase ~= Player.NotActive and player:getMark("emo__jingbu_me-turn") == 0 then
      return 3
    end
  end,
}
emo__jingbu:addRelatedSkill(emo__jingbu_maxcards)

local emo__jingbu_prohibit = fk.CreateProhibitSkill{
  name = "#emo__jingbu_prohibit",
  is_prohibited = function(self, from, to, card)
    return from and from.phase ~= Player.NotActive and card.type ~= Card.TypeEquip and from:getMark("emo__jingbu_other-turn") == 0
    and to:hasSkill(emo__jingbu) and from ~= to
  end,
}
emo__jingbu:addRelatedSkill(emo__jingbu_prohibit)

moesp__caveira:addSkill(emo__jingbu)

local emo__shenwen = fk.CreateTriggerSkill{
  name = "emo__shenwen",
  anim_type = "offensive",
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player ~= target and player.phase ~= Player.NotActive
    and player:usedSkillTimes(self.name, Player.HistoryTurn) < 1
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#emo__shenwen-invoke:"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, {target.id})
    local choices = {"#emo__shenwen_true", "#emo__shenwen_false"}
    local guess = room:askForChoice(player, choices, self.name, "#emo__shenwen-choice:"..target.id) == choices[1]
    local has = table.find(target:getCardIds("h"), function (id)
      return Fk:getCardById(id).name == "peach" or Fk:getCardById(id).name == "analeptic"
    end) ~= nil
    if not target.dead then
      local card = room:askForCardChosen(player, target, { card_data = { { "$Hand", target.player_cards[Player.Hand] } } }, self.name,
      "#emo__shenwen-throw:"..target.id)
      room:throwCard(card, self.name, target, player)
    end
    if guess == has then
      data.extra_data = data.extra_data or {}
      data.extra_data.emo__shenwen_from = player.id
    end
  end,
}

local emo__shenwen_delay = fk.CreateTriggerSkill{
  name = "#emo__shenwen_delay",
  anim_type = "offensive",
  events = {fk.AfterDying},
  can_trigger = function(self, event, target, player, data)
    return data.extra_data and data.extra_data.emo__shenwen_from == player.id and data.extra_data.emo__shenwen_victims
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("emo__shenwen")
    room:delay(600)
    local tos = data.extra_data.emo__shenwen_victims
    room:sortPlayersByAction(tos)
    for _, pid in ipairs(tos) do
      local to = room:getPlayerById(pid)
      if not to.dead then
        room:doIndicate(player.id, {target.id})
        room:damage { from = player, to = to, damage = 1, skillName = self.name }
      end
    end
  end,

  refresh_events = {fk.HpRecover},
  can_refresh = function (self, event, target, player, data)
    if player == data.recoverBy then
      local e = player.room.logic:getCurrentEvent():findParent(GameEvent.Dying)
      if e then
        local dying = e.data[1]
        return dying.extra_data and dying.extra_data.emo__shenwen_from and target.id == dying.who
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local e = player.room.logic:getCurrentEvent():findParent(GameEvent.Dying)
    if e then
      local dying = e.data[1]
      local tos = dying.extra_data.emo__shenwen_victims or {}
      table.insertIfNeed(tos, player.id)
      dying.extra_data.emo__shenwen_victims = tos
    end
  end,
}
emo__shenwen:addRelatedSkill(emo__shenwen_delay)

moesp__caveira:addSkill(emo__shenwen)

Fk:loadTranslationTable{
  ["moesp__caveira"] = "Caveira", -- Caveira
  ["#moesp__caveira"] = "女鬼",
  ["designer:moesp__caveira"] = "Kasa",
  
  ["emo__sujia"] = "速甲",
  [":emo__sujia"] = "锁定技，跳过你的摸牌阶段。准备阶段，你摸三张牌。",

  ["emo__jingbu"] = "静步",
  [":emo__jingbu"] = "锁定技，你的回合内，若你本回合未使用装备牌，你手牌上限+3，使用牌无距离限制且不可被响应。其他角色于其回合内使用的第一张非装备牌无法指定你为目标。",

  ["emo__shenwen"] = "审问",
  [":emo__shenwen"] = "每回合限一次，当一名其他角色于你的回合内进入濒死状态时，你可以猜测该角色手牌中是否有【桃】或【酒】，然后观看其手牌并弃置其中一张，若你猜对，你于濒死结算后对本次结算中令其回复体力的角色各造成1点伤害。",
  ["#emo__shenwen-invoke"] = "你可以“审问” %src ！猜测手牌中是否有【桃】或【酒】",
  ["#emo__shenwen-throw"] = "审问：观看 %src 手牌并弃置一张",
  ["#emo__shenwen_delay"] = "审问",
  ["#emo__shenwen-choice"] = "审问：猜测 %src 手牌中是否有【桃】或【酒】",
  ["#emo__shenwen_true"] = "有！",
  ["#emo__shenwen_false"] = "没有",

  ["$emo__shenwen1"] = "You're mine, palhaço.",
  ["$emo__shenwen2"] = "You're all mine, seu miserável!",
}

local moesp__daqiao = General(extension, "moesp__daqiao", "moe", 3, 3, General.Female)
moesp__daqiao.subkingdom = "wu"

local emo__chaofeng = fk.CreateActiveSkill{
  name = "emo__chaofeng",
  anim_type = "drawcard",
  card_num = 0,
  target_num = 0,
  card_filter = Util.FalseFunc,
  prompt = "#emo__chaofeng",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    if player.dead then return end
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".",
    }
    room:judge(judge)
    if judge.card.color == Card.Red then
      player:drawCards(2, self.name)
    elseif judge.card.color == Card.Black then
      room:askForGuanxing(player, room:getNCards(2))
    end
  end,
}
moesp__daqiao:addSkill(emo__chaofeng)

local emo__jianming = fk.CreateTriggerSkill{
  name = "emo__jianming",
  anim_type = "drawcard",
  events = {fk.Damage},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.extra_data and data.extra_data.emo__jianming_check
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for i = 1, data.extra_data.emo__jianming_check do
      emo__chaofeng:onUse(room, {
        from = player.id,
        cards = {},
        tos = {},
      })
    end
  end,

  -- 全局记录器
  refresh_events = {fk.Damage},
  can_refresh = function (self, event, target, player, data)
    return player == target and target.gender == General.Female
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local sum = (room:getTag("emo__jianming_count") or 0) + data.damage
    local n = sum // 3
    room:setTag("emo__jianming_count", sum % 3)
    if n > 0 then
      data.extra_data = data.extra_data or {}
      data.extra_data.emo__jianming_check = n
    end
  end,
}
moesp__daqiao:addSkill(emo__jianming)

local emo__kongti = fk.CreateTriggerSkill{
  name = "emo__kongti",
  anim_type = "offensive",
  events = {fk.RoundStart, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.RoundStart then
      return player:hasSkill(self) and not player:isNude()
    else
      return player:hasSkill(self) and table.contains(player:getTableMark("emo__kongti-round"), target.id)
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TurnStart then return true end
    local room = player.room
    local x = room:getTag("RoundCount")
    local ids = table.filter(player:getCardIds("he"), function(id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
    local tos, cards = room:askForChooseCardsAndPlayers(player, x, x, table.map(room:getOtherPlayers(player), Util.IdMapper), 
    1, x, tostring(Exppattern{ id = ids }), "#emo__kongti-invoke:::"..x)
    if #tos > 0 and #cards == x then
      room:sortPlayersByAction(tos)
      self.cost_data = {tos, cards}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local tos, cards = table.unpack(self.cost_data)
      room:setPlayerMark(player, "emo__kongti-round", tos)
      room:throwCard(cards, self.name, player, player)
      for _, pid in ipairs(tos) do
        local to = room:getPlayerById(pid)
        if not to.dead then
          room:damage { from = player, to = to, damage = 1, skillName = self.name, damageType = fk.FireDamage }
        end
      end
    else
      room:doIndicate(player.id, {target.id})
      emo__chaofeng:onUse(room, {
        from = player.id,
        cards = {},
        tos = {},
      })
    end
  end,
}
moesp__daqiao:addSkill(emo__kongti)

Fk:loadTranslationTable{
  ["moesp__daqiao"] = "珺瑶大乔",
  ["#moesp__daqiao"] = "瑶池台祭司",
  ["designer:moesp__daqiao"] = "贾文和",
  
  ["emo__chaofeng"] = "朝凤",
  [":emo__chaofeng"] = "出牌阶段限一次，你可以进行一次判定，若为红色，你摸两张牌，若为黑色，你卜算2。",
  ["#emo__chaofeng"] = "朝凤：你可判定，若为红则摸2张牌，为黑则观星2张",

  ["emo__jianming"] = "涧鸣",
  [":emo__jianming"] = "锁定技，当女性角色每累计造成3点伤害后，你发动一次〖朝凤〗。",

  ["emo__kongti"] = "空啼",
  [":emo__kongti"] = "每轮开始时，你可以弃置X张牌，对至多X名其他角色各造成1点火焰伤害，且本轮中这些角色的回合开始时，你发动一次〖朝凤〗（X为本轮轮数）。",
  ["#emo__kongti-invoke"] = "空啼：你可弃置 %arg 张牌，对至多 %arg 名其他角色各造成1点火焰伤害",
}

local jiyuan = General(extension, "moesp__jiyuan", "moe", 4, 4, General.Female)

local emo__dieren = fk.CreateTriggerSkill{
  name = "emo__dieren",
  anim_type = "offensive",
  events = {fk.CardEffectCancelledOut, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == fk.CardEffectCancelledOut then
        return data.card.trueName == "slash"
      else
        return player:getMark("@emo__dieren") > 0
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.CardEffectCancelledOut then
      self.cost_data = nil
      return true
    else
      local prompt = "#emo__dieren-invoke:"
      if player:getMark("emo__dieren_upgraded") > 0 then
        prompt = "#emo__dieren_up-invoke:"
      end
      if player.room:askForSkillInvoke(player, self.name, nil, prompt..data.to.id.."::"..player:getMark("@emo__dieren")) then
        self.cost_data = {tos = {data.to.id}}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardEffectCancelledOut then
      room:addPlayerMark(player, "@emo__dieren", 1)
    else
      data.damage = data.damage + player:getMark("@emo__dieren")
      if player:getMark("emo__dieren_upgraded") == 0 then
        room:setPlayerMark(player, "@emo__dieren", 0)
      end
    end
  end,
}
jiyuan:addSkill(emo__dieren)


local emo__chaoyuan = fk.CreateTriggerSkill{
  name = "emo__chaoyuan",
  events = {fk.EnterDying},
  frequency = Skill.Wake,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  can_wake = function (self, event, target, player, data)
    return target == player or (data.damage and data.damage.from == player)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, -1)
    if player.dead then return end
    room:recover { num = 3, skillName = self.name, who = player, recoverBy = player }
    room:setPlayerMark(player, "emo__dieren_upgraded", 1)
    room:handleAddLoseSkills(player, "emo__guangren|emo__yuanren|emo__lieren")
  end,
}
jiyuan:addSkill(emo__chaoyuan)

local emo__guangren = fk.CreateActiveSkill{
  name = "emo__guangren",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  prompt = "#emo__guangren",
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    room:damage { from = player, to = to, damage = 1, skillName = self.name }
  end,
}
jiyuan:addRelatedSkill(emo__guangren)

local emo__yuanren = fk.CreateTriggerSkill{
  name = "emo__yuanren",
  anim_type = "defensive",
  events = {fk.AfterCardsMove},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    local n = 0
    for _, move in ipairs(data) do
      if move.from == player.id and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
            if Fk:getCardById(info.cardId).trueName == "slash" then
              n = n + 1
            end
          end
        end
      end
    end
    if n > 0 then
      self.cost_data = {num = n}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:changeShield(player, self.cost_data.num)
  end,
}
jiyuan:addRelatedSkill(emo__yuanren)

local emo__lieren = fk.CreateTriggerSkill{
  name = "emo__lieren",
  events = {fk.CardUseFinished},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player and data.card.trueName == "slash" and not player:isKongcheng() then
      local useEvents = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data[1]
        return use.from == player.id and use.card.trueName == "slash"
      end, Player.HistoryTurn)
      return #useEvents > 0 and useEvents[1].data[1] == data
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local slash = Fk:cloneCard(data.card.name)
    slash.skillName = self.name
    local tos = table.filter(TargetGroup:getRealTargets(data.tos), function (pid)
      local p = room:getPlayerById(pid)
      return not p.dead and not player:isProhibited(p, slash)
    end)
    if #tos == 0 then return end
    local cards = room:askForDiscard(player, 1, 1, false, self.name, true, ".|.|.|hand", "#emo__lieren-card", true)
    if #cards > 0 then
      self.cost_data = {cards = cards, tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(self.cost_data.cards, self.name, player, player)
    room:useVirtualCard(data.card.name, nil, player, table.map(self.cost_data.tos, Util.Id2PlayerMapper), self.name, true)
  end,
}
jiyuan:addRelatedSkill(emo__lieren)


Fk:loadTranslationTable{
  ["moesp__jiyuan"] = "纪源",
  ["#moesp__jiyuan"] = "新纪起源",
  ["designer:moesp__jiyuan"] = "冯世淇",
  
  ["emo__dieren"] = "叠刃",
  [":emo__dieren"] = "每当你使用【杀】被抵消后，获得1枚“叠刃”；每当你造成伤害时，你可以移去所有“叠刃”增加等量伤害值。",
  ["@emo__dieren"] = "叠刃",
  ["#emo__dieren-invoke"] = "叠刃：你可以移去所有“叠刃”，令你对 %src 造成伤害+%arg",
  ["#emo__dieren_up-invoke"] = "叠刃：你可以令你对 %src 造成伤害+%arg",

  ["emo__chaoyuan"] = "超源",
  [":emo__chaoyuan"] = "觉醒技，当你或你令一名角色进入濒死状态时，扣减一点体力上限并回复三点体力，获得技能〖光刃〗，〖源刃〗，〖连刃〗，并升级〖叠刃〗：你造成伤害时不移去“叠刃”。",

  ["emo__guangren"] = "光刃",
  [":emo__guangren"] = "出牌阶段限一次，你可以对一名角色造成1点伤害。",
  ["#emo__guangren"] = "光刃：选择1名角色，你对其造成1点伤害",

  ["emo__yuanren"] = "源刃",
  [":emo__yuanren"] = "锁定技，每当你的一张【杀】被弃置后，你获得1点护甲。",

  ["emo__lieren"] = "连刃",
  [":emo__lieren"] = "你每回合使用的首张【杀】结算后，你可以弃置一张手牌，视为对相同目标使用一张同牌名的【杀】。",
  ["#emo__lieren-card"] = "连刃：你可弃置一张手牌，视为再出一张【杀】！",
}

local caoying = General(extension, "moesp__caoying", "moe", 4, 4, General.Female)
caoying.subkingdom = "wei"

local lingren = fk.CreateTriggerSkill{
  name = "emo__lingren",
  anim_type = "offensive",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:usedSkillTimes(self.name) < 2 and
    data.firstTarget and data.card.is_damage_card
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(AimGroup:getAllTargets(data.tos), function (id)
      return not room:getPlayerById(id).dead
    end)
    if #targets == 1 then
      if room:askForSkillInvoke(player, self.name, nil, "#lingren-invoke::" .. targets[1]) then
        self.cost_data = {tos = targets}
        return true
      end
    elseif #targets > 1 then
      targets = room:askForChoosePlayers(player, targets, 1, 1, "#lingren-choose", self.name, true, false)
      if #targets > 0 then
        self.cost_data = {tos = targets}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    local choices = {"lingren_basic", "lingren_trick", "lingren_equip"}
    local yes = room:askForChoices(player, choices, 0, 3, self.name, "#lingren-choice::" .. to.id, false)
    for _, value in ipairs(yes) do
      table.removeOne(choices, value)
    end
    local right = 0
    for _, id in ipairs(to.player_cards[Player.Hand]) do
      local str = "lingren_"..Fk:getCardById(id):getTypeString()
      if table.contains(yes, str) then
        right = right + 1
        table.removeOne(yes, str)
      else
        table.removeOne(choices, str)
      end
    end
    right = right + #choices
    room:sendLog{
      type = "#lingren_result",
      from = player.id,
      arg = tostring(right),
      toast = true,
    }
    if right == 0 then return end
    if data.to == to.id then
      data.additionalDamage = (data.additionalDamage or 0) + 1
      if right > 1 then
        data.disresponsive = true
      end
    else
      -- 我恨延时延时传输数据……
      data.extra_data = data.extra_data or {}
      data.extra_data.emo__lingrenRecord = data.extra_data.emo__lingrenRecord or {}
      data.extra_data.emo__lingrenRecord[tostring(to.id)] = right
    end
    if right > 1 then
      player:drawCards(2, self.name)
    end
    if right > 2 then
      local skills = table.filter({"tycl__jianxiong", "m_ex__xingshang"}, function (s)
        return not player:hasSkill(s, true)
      end)
      if #skills > 0 then
        room:setPlayerMark(player, self.name, skills)
        room:handleAddLoseSkills(player, table.concat(skills, "|"), nil, true, false)
      end
    end
  end,
}

local lingren_delay = fk.CreateTriggerSkill {
  name = "#emo__lingren_delay",
  mute = true,
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if player.dead or target ~= player then return false end
    return data.extra_data and (data.extra_data.emo__lingrenRecord or {})[tostring(data.to)]
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local right = data.extra_data.emo__lingrenRecord[tostring(data.to)]
    data.additionalDamage = (data.additionalDamage or 0) + 1
    if right > 1 then
      data.disresponsive = true
    end
  end,

  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark(lingren.name) ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local skills = player:getMark(lingren.name)
    room:setPlayerMark(player, lingren.name, 0)
    room:handleAddLoseSkills(player, "-"..table.concat(skills, "|-"), nil, true, false)
  end,
}
lingren:addRelatedSkill(lingren_delay)
caoying:addSkill(lingren)

local fujian = fk.CreateTriggerSkill {
  name = "emo__fujian",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.GameStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.map(room:getOtherPlayers(player, false), Util.IdMapper)
    if #targets == 0 then return false end
    local tos = #targets == 1 and targets or
    room:askForChoosePlayers(player, targets, 1, 1, "#emo__fujian-choose", self.name, false)
    self.cost_data = {tos = tos}
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    room:addPlayerMark(to, "@@emo__fujian_tar")
    room:addTableMark(player, "emo__fujian_record", to.id)
    -- FIXEME: buddy 应该重构……
    player:addBuddy(to)
  end,

  refresh_events = {fk.EventLoseSkill, fk.BuryVictim},
  can_refresh = function(self, event, target, player, data)
    if player:getMark("emo__fujian_record") == 0 or target ~= player then return false end
    if event == fk.EventLoseSkill then
      return data == self
    else
      return target:hasSkill(self, true, true)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, pid in ipairs(player:getMark("emo__fujian_record")) do
      local to = room:getPlayerById(pid)
      room:removePlayerMark(to, "@@emo__fujian_tar")
      --player:removeBuddy(to)
    end
  end,
}

local fujian_targetmod = fk.CreateTargetModSkill{
  name = "#emo__fujian_targetmod",
  bypass_distances = function(self, player, skill, card, to)
    return card and player and to and table.contains(player:getTableMark("emo__fujian_record"), to.id)
  end,
}
fujian:addRelatedSkill(fujian_targetmod)


caoying:addSkill(fujian)

caoying:addRelatedSkill("tycl__jianxiong")
caoying:addRelatedSkill("m_ex__xingshang")

Fk:loadTranslationTable{
  ["moesp__caoying"] = "曹婴",
  ["#moesp__caoying"] = "龙城凤鸣",
  ["designer:moesp__caoying"] = "北杏",
  
  ["emo__lingren"] = "凌人",
  [":emo__lingren"] = "每回合限两次，当你使用伤害牌指定目标后，你可以猜测一个目标角色的手牌是否有基本牌、锦囊牌或装备牌。至少猜对一项则此牌对其伤害+1；至少猜对两项则其无法响应此牌；猜对三项则你获得〖奸雄〗和〖行殇〗直到你下回合开始。",

  ["emo__fujian"] = "伏间",
  [":emo__fujian"] = "锁定技，游戏开始时，你令一名其他角色获得“敌”标记：其手牌始终对你可见，且你对其使用【杀】无距离限制。",
  ["@@emo__fujian_tar"] = "敌",
  ["#emo__fujian-choose"] = "伏间：令一名其色获得“敌”：其手牌对你可见，你对其用【杀】无距离限制",

  ["$emo__lingren1"] = "秉凌人之盛气，试青锋于万里！",
  ["$emo__lingren2"] = "斩宵小于沙场，绝仇雠于疆野！",
  ["$emo__fujian1"] = "紫电耀魍魉之遁形，青霜斩魑魅之神魄。",
  ["$emo__fujian2"] = "纵汝亡命天涯海角，吾亦逐之负首而还。",
  ["$tycl__jianxiong_moesp__caoying"] = "太公曾教我一言，宁许我负人，不许人负我！",
  ["$m_ex__xingshang_moesp__caoying"] = "尔等且往黄泉，此物留于人间，自可为我所用。",
  ["~moesp__caoying"] = "黄泉羞见太祖……父兄……",
}

local moesp__kaluoer = General(extension, "moesp__kaluoer", "moe", 4, 4, General.Female)

local emo__huizhen = fk.CreateTriggerSkill{
  name = "emo__huizhen",
  anim_type = "support",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if player:usedSkillTimes(self.name, Player.HistoryRound) > 0 or not player:hasSkill(self) then return end
    local mark = target:getMark("@emo__huizhen-turn")
    if mark ~= 0 then
      local hand, hp = table.unpack(table.map(mark, function(e) return tonumber(string.split(e, ":")[2]) end))
      return target:getHandcardNum() == hand and target.hp == hp
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name, nil, "#emo__huizhen-invoke:" .. target.id) then
      self.cost_data = {tos = {target.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@emo__timeMark")
    target:gainAnExtraTurn(false, self.name)
  end,

  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return not target.dead and target:getMark("@emo__huizhen-turn") == 0
    and player:hasSkill(self, true) and player:usedSkillTimes(self.name, Player.HistoryRound) == 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(target, "@emo__huizhen-turn", {"牌:"..target:getHandcardNum(), "血:"..target.hp})
  end,
}
moesp__kaluoer:addSkill(emo__huizhen)

local emo__shixing = fk.CreateTriggerSkill{
  name = "emo__shixing",
  frequency = Skill.Wake,
  events = { fk.TurnEnd, fk.EnterDying },
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  can_wake = function (self, event, target, player, data)
    if event == fk.TurnEnd then
      return player:getMark("@emo__timeMark") > 2
    else
      return target == player and player:hasSkill("emo__huizhen", true)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EnterDying then
      room:handleAddLoseSkills(player, "-emo__huizhen")
    end
    room:recover { num = 3, skillName = self.name, who = player , recoverBy = player}
    room:handleAddLoseSkills(player, "emo__xinshi|emo__xinhui")
  end,
}

local emo__shixing_delay = fk.CreateTriggerSkill{
  name = "#emo__shixing_delay",
  events = { fk.EventPhaseChanging },
  can_trigger = function(self, event, target, player, data)
    return target == player and data.to == Player.Draw and player:usedSkillTimes("emo__shixing", Player.HistoryGame) > 0
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#emo__shixing-skip")
  end,
  on_use = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "@emo__timeMark", 2)
    return true
  end,
}
emo__shixing:addRelatedSkill(emo__shixing_delay)

moesp__kaluoer:addSkill(emo__shixing)

local emo__xinshi = fk.CreateTriggerSkill{
  name = "emo__xinshi",
  events = {fk.EventPhaseStart},
  anim_type = "support",  can_trigger = function(self, event, target, player, data)
    return not target.dead and target.phase == Player.Start and player:hasSkill(self) and player:getMark("@emo__timeMark") > 0
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name, nil, "#emo__xinshi-invoke:"..target.id) then
      self.cost_data = {tos = {target.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:removePlayerMark(player, "@emo__timeMark")
    if not target:isNude() then
      local cards = room:askForCard(target, 1, 9999, true, self.name, true, ".", "#emo__xinshi-recast")
      if #cards > 0 then
        room:recastCard(cards, target, self.name)
      end
    end
    if not target.dead then
      target:gainAnExtraPhase(Player.Play, true)
    end
  end,
}
moesp__kaluoer:addRelatedSkill(emo__xinshi)


local emo__xinhui = fk.CreateTriggerSkill{
  name = "emo__xinhui",
  events = {fk.EventPhaseStart},
  anim_type = "support",  can_trigger = function(self, event, target, player, data)
    if not target.dead and target.phase == Player.Finish and player:hasSkill(self) and player:getMark("@emo__timeMark") > 0 then
      local ids = {}
      player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data[1]
        if use.from == target.id then
          table.insertTableIfNeed(ids, Card:getIdList(use.card))
        end
        return false
      end, Player.HistoryTurn)
      ids = table.filter(ids, function (id) return player.room:getCardArea(id) == Card.DiscardPile end)
      if #ids > 0 then
        self.cost_data = ids
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cards = self.cost_data
    if player.room:askForSkillInvoke(player, self.name, nil, "#emo__xinhui-invoke:"..target.id.."::"..#cards) then
      self.cost_data = {cards = cards, tos = {target.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:removePlayerMark(player, "@emo__timeMark")
    player.room:obtainCard(target, self.cost_data.cards, true, fk.ReasonPrey, player.id, self.name)
  end,
}
moesp__kaluoer:addRelatedSkill(emo__xinhui)


Fk:loadTranslationTable{
  ["moesp__kaluoer"] = "卡洛儿",
  ["#moesp__kaluoer"] = "甜密时光",
  ["designer:moesp__kaluoer"] = "小七",
  
  ["emo__huizhen"] = "回针",
  [":emo__huizhen"] = "每轮限一次，每当一名角色回合结束时，若其体力值与手牌数等于回合开始时，你可以获得1枚“时”标记并令其获得一个额外回合。",
  ["#emo__huizhen-invoke"] = "回针：你可获得1枚“时”并令 %src 获得一个额外回合",
  ["@emo__huizhen-turn"] = "回针",
  ["@emo__timeMark"] = "时",

  ["emo__shixing"] = "时醒",
  [":emo__shixing"] = "觉醒技，{①任意角色回合结束时，若你有3枚“时”标记；②你进入濒死状态时，失去〖回针〗}：你回复3点体力，并获得技能〖新始〗，〖新回〗，且本局你可跳过摸牌阶段以获得2枚“时”标记。",
  ["#emo__shixing_delay"] = "时醒",
  ["#emo__shixing-skip"] = "时醒：你可跳过摸牌阶段以获得2枚“时”标记",

  ["emo__xinshi"] = "新始",
  [":emo__xinshi"] = "一名角色准备阶段，你可以移去一个“时”标记，令其重铸任意张牌并执行一个额外的出牌阶段。",
  ["#emo__xinshi-invoke"] = "新始：你可弃“时”标记令 %src :重铸任意张牌，并执行出牌阶段",
  ["#emo__xinshi-recast"] = "新始：你可以重铸任意张牌",

  ["emo__xinhui"] = "新回",
  [":emo__xinhui"] = "一名角色的结束阶段，你可以移去一个“时”标记，令其从弃牌堆获得本回合使用过的牌。",
  ["#emo__xinhui-invoke"] = "新回：你可弃“时”标记令 %src 获得本回合使用过的牌(%arg张)",
}


local shark = General(extension, "moesp__specter_shark", "moe", 4, 4, General.Female)

local emo__xueju = fk.CreateTriggerSkill{
  name = "emo__xueju",
  anim_type = "offensive",
  events = { fk.Damage },
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and not data.to.dead
  end,
  on_cost = function (self, event, target, player, data)
    self.cost_data = {tos = {data.to.id}}
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(data.to, "@emo__fear", data.damage)
    local num = player:usedSkillTimes("emo__xuelu", Player.HistoryGame) == 0 and 5 or 3
    player:drawCards(data.to:getMark("@emo__fear"), self.name)
    if data.to:getMark("@emo__fear") >= num and data.to.faceup then
      room:removePlayerMark(data.to, "@emo__fear", num)
      data.to:turnOver()
    end
  end,
}
shark:addSkill(emo__xueju)

local emo__yesha = fk.CreateTriggerSkill{
  name = "emo__yesha",
  anim_type = "offensive",
  events = { fk.TurnStart },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and table.find(player.room.alive_players, function (p)
      return p ~= player and #p:getCardIds("e") > 0
    end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player), function (p) return #p:getCardIds("e") > 0 end)
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#emo__yesha-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    local cards = to:getCardIds("e")
    if #cards == 0 then return end
    room:throwCard(cards, self.name, to, player)
    if not to:isKongcheng() then
      room:throwCard(table.random(to:getCardIds("h"), #cards), self.name, to, player)
    end
    if not to.dead and player:usedSkillTimes("emo__xuelu", Player.HistoryGame) > 0 then
      room:damage { from = player, to = to, damage = #cards, skillName = self.name }
    end
  end,
}
shark:addSkill(emo__yesha)

local emo__xuelu = fk.CreateTriggerSkill{
  name = "emo__xuelu",
  frequency = Skill.Wake,
  events = {fk.Deathed},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  can_wake = function(self, event, target, player, data)
    return data.damage and data.damage.from == player
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:usedSkillTimes(self.name, Player.HistoryGame) <= 1 then
      room:handleAddLoseSkills(player, "emo__xueqian|emo__zhenchang")
    else
      room:addPlayerMark(player, "@emo__xuelu-turn", 1)
      room:changeMaxHp(player, 1)
    end
  end,

  refresh_events = {fk.DamageCaused},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("@emo__xuelu-turn") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    data.damage = data.damage + player:getMark("@emo__xuelu-turn")
  end,
}
shark:addSkill(emo__xuelu)

local emo__xueqian = fk.CreateTriggerSkill{
  name = "emo__xueqian",
  frequency = Skill.Compulsory,
  events = {fk.AfterDying},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player.phase ~= Player.NotActive and target ~= player and not target.dead
    and target:getMark("@@emo__xueqian") == 0
  end,
  on_cost = function (self, event, target, player, data)
    self.cost_data = {tos = {target.id}}
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if room:askForChoice(player, {"recover", "#emo__xueqian_prevent:"..target.id}, self.name) == "recover" then
      room:recover { num = 1, skillName = self.name, who = player, recoverBy = player }
    else
      room:setPlayerMark(target, "@@emo__xueqian", 1)
    end
  end,
}

local emo__xueqian_delay = fk.CreateTriggerSkill{
  name = "#emo__xueqian_delay",
  frequency = Skill.Compulsory,
  events = {fk.PreHpRecover},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and target:getMark("@@emo__xueqian") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "@@emo__xueqian", 0)
    room:doAnimate("InvokeSkill", { name = "emo__xueqian", player = player.id, skill_type = "negative" })
    room:sendLog{ type = "#PreventRecover", from = player.id, arg = "emo__xueqian" }
    return true
  end,
}
emo__xueqian:addRelatedSkill(emo__xueqian_delay)

shark:addRelatedSkill(emo__xueqian)

local emo__zhenchang = fk.CreateTriggerSkill{
  name = "emo__zhenchang",
  frequency = Skill.Limited,
  events = {fk.EventPhaseStart},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player.phase == Player.Finish and target == player
    and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#emo__zhenchang-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:turnOver()
    local targets = room:getOtherPlayers(player)
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    for _, p in ipairs(targets) do
      if not p.dead then
        room:damage { from = player, to = p, damage = 1, skillName = self.name }
        room:delay(200)
      end
    end
  end,
}
shark:addRelatedSkill(emo__zhenchang)


Fk:loadTranslationTable{
  ["moesp__specter_shark"] = "夜修鱼",
  ["#moesp__specter_shark"] = "夜世游鲨",
  ["designer:moesp__specter_shark"] = "封世七长久",
  
  ["emo__xueju"] = "血锯",
  [":emo__xueju"] = "锁定技，每当你对其他角色造成伤害后，令其获得伤害值枚“惧”标记，你摸其“惧”数张牌，若其正面朝上且有至少5枚“惧”，移去5枚令其翻面。",
  ["@emo__fear"] = "惧",

  ["emo__yesha"] = "夜鲨",
  [":emo__yesha"] = "回合开始时，你可以弃置一名其他角色装备区中所有牌，并随机弃置其等量张手牌。",
  ["#emo__yesha-choose"] = "夜鲨：选择一名其他角色，弃置其所有装备，弃置其等量张手牌！",

  ["emo__xuelu"] = "血戮",
  [":emo__xuelu"] = "觉醒技，当你击杀一名角色后，获得技能〖血浅〗、〖震场〗，并升级〖夜鲨〗（再对其造成等量伤害）、升级〖血锯〗（5枚改为3枚）。"..
  "<br><b>可多次觉醒，</b>第二次起：增加1点体力上限且本回合造成伤害+1。",
  ["@emo__xuelu-turn"] = "血戮:伤害+",

  ["emo__xueqian"] = "血浅",
  [":emo__xueqian"] = "锁定技，其他角色于你回合内脱离濒死后，你选择回复1点体力或防止其下次回复体力。",
  ["#emo__xueqian_prevent"] = "防止 %src 下次回复体力",
  ["@@emo__xueqian"] = "浅",
  ["#emo__xueqian_delay"] = "血浅",
  ["#PreventRecover"] = "%from 回复体力被 %arg 防止",

  ["emo__zhenchang"] = "震场",
  [":emo__zhenchang"] = "限定技，结束阶段，你可以翻面，并依次对所有其他角色造成1点伤害。",
  ["#emo__zhenchang-invoke"] = "震场：你可以翻面，并依次对所有其他角色造成1点伤害",

  ["$emo__xueju"] = "额呵呵……",
  ["$emo__yesha1"] = "我来自深邃的海洋……",
  ["$emo__yesha2"] = "回归深渊吧！",
  ["$emo__xuelu1"] = "呵呵呵…哈哈……",
  ["$emo__xuelu2"] = "哈哈哈哈！！",
  ["$emo__xueqian"] = "不许逃走。",
  ["$emo__zhenchang1"] = "各位，晚安……",
  ["$emo__zhenchang2"] = "各位，晚安……",
}

local luoweiliu = General(extension, "moesp__luoweiliu", "moe", 4, 4, General.Female)

local emo__wangjing = fk.CreateTriggerSkill{
  name = "emo__wangjing",
  anim_type = "control",
  events = { fk.TurnStart },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and table.find(player.room.alive_players, function (p)
      return p ~= player and not p:isKongcheng()
    end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player), function (p) return not p:isKongcheng() end)
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#emo__wangjing-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    local cards = to:getCardIds("h")
    if #cards == 0 then return end
    local x = math.max(0, player.hp)
    local ok_buttons, cancel_button = {}, {"Cancel"}
    if x > 0 then
      table.insert(ok_buttons, "OK")
      if x >= #cards then
        table.insert(cancel_button, 1, "All")
      end
    end
    local chosen, choice = U.askforChooseCardsAndChoice(player, cards, ok_buttons, self.name, "#emo__wangjing-mark:::"..x,
    cancel_button, x > 0 and 1 or 0, x)
    if choice == "All" then
      chosen = cards
    end
    for _, id in ipairs(chosen) do
      room:setCardMark(Fk:getCardById(id), "@@emo__meteor", 1)
    end
  end,

  -- 监测流星标记的移动，应该写在赋予流星标记的技能处
  refresh_events = {fk.AfterCardsMove},
  can_refresh = function (self, event, target, player, data)
    if player ~= player.room.players[1] then return end -- 仅侦测一次
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if Fk:getCardById(info.cardId):getMark("@@emo__meteor") ~= 0 then
          return true
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local map = {}
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if Fk:getCardById(info.cardId):getMark("@@emo__meteor") ~= 0 then
          room:setCardMark(Fk:getCardById(info.cardId), "@@emo__meteor", 0)
          if move.from and (move.moveReason == fk.ReasonUse or move.moveReason == fk.ReasonDiscard) then
            map[tostring(move.from)] = (map[tostring(move.from)] or 0) + 1
          end
        end
      end
    end
    if next(map) ~= nil then
      data.extra_data = data.extra_data or {}
      data.extra_data.emo__meteor = map
    end
  end,
}
luoweiliu:addSkill(emo__wangjing)

local emo__zhuixing = fk.CreateTriggerSkill{
  name = "emo__zhuixing",
  anim_type = "offensive",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.extra_data and data.extra_data.emo__meteor
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local map = data.extra_data.emo__meteor
    for pid, num in pairs(map) do
      if not player:hasSkill(self) then break end
      local p = room:getPlayerById(tonumber(pid))
      if not p.dead then
        self:doCost(event, p, player, num)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    self.cost_data = {tos = {target.id}}
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(target, "@emo__starFall", data)
    if target:getMark("@emo__starFall") >= 5 then
      room:removePlayerMark(target, "@emo__starFall", 5)
      room:delay(300)
      room:doIndicate(player.id, {target.id})
      room:damage { from = player, to = target, damage = 3, skillName = self.name }
    end
  end,
}
luoweiliu:addSkill(emo__zhuixing)

local emo__xingtu = fk.CreateTriggerSkill{
  name = "emo__xingtu",
  anim_type = "control",
  events = { fk.CardUseFinished },
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and player.phase ~= Player.NotActive then
      local cardType = player:getMark("@emo__xingtu-turn")
      if cardType == 0 then
        local useEvents = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
          local use = e.data[1]
          return use.from == player.id
        end, Player.HistoryTurn)
        if #useEvents == 0 then return false end
        cardType = useEvents[1].data[1].card:getTypeString()
        player.room:setPlayerMark(player, "@emo__xingtu-turn", cardType)
      end
      return cardType == data.card:getTypeString() and table.find(player.room.alive_players, function (p)
        return p ~= player and not p:isKongcheng()
      end) 
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player), function (p) return not p:isKongcheng() end)
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#emo__xingtu-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    local cards = to:getCardIds("h")
    if #cards == 0 then return end
    local card = Fk:getCardById(table.random(cards))
    local isStar = card:getMark("@@emo__meteor") > 0
    room:throwCard(card, self.name, to, player)
    if isStar then
      if to.dead then return end
      room:addPlayerMark(to, "@emo__starFall")
      if to:getMark("@emo__starFall") >= 5 then
        local from = table.find(room:getAlivePlayers(), function (p) return p:hasSkill("emo__zhuixing") end)
        if from then
          room:removePlayerMark(to, "@emo__starFall", 5)
          room:notifySkillInvoked(player, "emo__zhuixing", "offensive", {to.id})
          room:doIndicate(player.id, {to.id})
          room:damage { from = player, to = to, damage = 3, skillName = "emo__zhuixing" }
        end
      end
    else
      if not player.dead then
        player:drawCards(1, self.name)
      end
    end
  end,
}
luoweiliu:addSkill(emo__xingtu)

Fk:loadTranslationTable{
  ["moesp__luoweiliu"] = "洛微流",
  ["#moesp__luoweiliu"] = "观星录图",
  ["designer:moesp__luoweiliu"] = "世世无常",
  
  ["emo__wangjing"] = "望镜",
  [":emo__wangjing"] = "回合开始时，你可以观看一名其他角色的手牌，选择其中至多X张牌标记为“流星”牌（X为你当前体力值）。",
  ["@@emo__meteor"] = "流星",
  ["#emo__wangjing-choose"] = "望镜：你可观看一名其他角色的手牌，并标记为“流星”牌",
  ["#emo__wangjing-mark"] = "选择至多 %arg 张牌标记为“流星”",
  ["All"] = "全部",

  ["emo__zhuixing"] = "坠星",
  [":emo__zhuixing"] = "锁定技，当一名角色使用或弃置一张“流星”牌后，令其获得1枚“坠星”标记；当任意角色有5枚“坠星”标记，移去并对其造成3点伤害。",
  ["@emo__starFall"] = "坠星",

  ["emo__xingtu"] = "星图",
  [":emo__xingtu"] = "你的回合内，每当你使用与本回合第一张牌类型相同的牌后，你可以弃置一名其他角色随机一张手牌，若此牌为“流星”牌，令其获得1枚“坠星”标记；若不为，你摸一张牌。",
  ["#emo__xingtu-choose"] = "星图：你可弃置一名其他角色随机一张手牌",
  ["@emo__xingtu-turn"] = "星图",
}

local foxwish = General(extension, "moesp__foxwish", "moe", 4, 4, General.Female)

local emo__guihuo = fk.CreateTriggerSkill{
  name = "emo__guihuo",
  events = {fk.Damaged, fk.CardUsing, fk.CardResponding},
  frequency = Skill.Compulsory,
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target ~= player and not target.dead then
      if event == fk.Damaged then
        return true
      else
        return target:getMark("@emo__ghostFire") > 0 and not target:isNude()
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    self.cost_data = {tos = {target.id}}
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damaged then
      local num = (player:usedSkillTimes("emo__yeming", Player.HistoryGame) > 0) and 2 or 1
      room:addPlayerMark(target, "@emo__ghostFire", num)
    else
      local cards = target:getCardIds("he")
      if #cards == 0 then return end
      room:removePlayerMark(target, "@emo__ghostFire")
      room:throwCard(table.random(cards), self.name, target, target)
    end
  end,
}
foxwish:addSkill(emo__guihuo)


local emo__yedeng = fk.CreateTriggerSkill{
  name = "emo__yedeng",
  events = {fk.AfterCardsMove},
  frequency = Skill.Compulsory,
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      for _, move in ipairs(data) do
        if move.moveReason == fk.ReasonDiscard and move.skillName == "emo__guihuo" then
          if #move.moveInfo > 0 then
            self.cost_data = move.moveInfo[1].cardId
            return true
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:delay(200)
    local cardId = self.cost_data
    if player:usedSkillTimes("emo__yeming", Player.HistoryGame) > 0 and room:getCardArea(cardId) == Card.DiscardPile then
      room:obtainCard(player, cardId, true, fk.ReasonJustMove, player.id, self.name)
    end
    if player.dead then return end
    room:addPlayerMark(player, "@emo__nightLight")
    local num = player:getMark("@emo__nightLight")
    if num % 3 == 0 then
      player:drawCards(1, self.name)
    end
    if num >= 9 and not player.dead then
      room:setPlayerMark(player, "@emo__nightLight", 0)
      room:changeMaxHp(player, 1)
      room:recover { num = 1, skillName = self.name, who = player, recoverBy = player }
    end
  end,
}
foxwish:addSkill(emo__yedeng)

local emo__yeming = fk.CreateTriggerSkill{
  name = "emo__yeming",
  frequency = Skill.Wake,
  events = {fk.MaxHpChanged},
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and data.num > 0
    and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  can_wake = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    player.room:handleAddLoseSkills(player, "emo__nosleep")
  end,
}
foxwish:addSkill(emo__yeming)

-- “不夜”和“布野”重名了……
local emo__nosleep = fk.CreateActiveSkill{
  name = "emo__nosleep",
  anim_type = "offensive",
  card_num = 0,
  target_num = 0,
  frequency = Skill.Limited,
  card_filter = Util.FalseFunc,
  prompt = "#emo__nosleep",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    for _, p in ipairs(room:getOtherPlayers(player)) do
      local num = p:getMark("@emo__ghostFire")
      if num > 0 then
        room:setPlayerMark(p, "@emo__ghostFire", 0)
        local cards = p:getCardIds("he")
        if #cards > 0 then
          room:delay(200)
          room:throwCard(table.random(cards, num), self.name, p, p)
        end
      end
    end
    local num = player:getMark("@emo__nightLight")
    room:setPlayerMark(player, "@emo__nightLight", 0)
    for i = 1, num do
      local others = room:getOtherPlayers(player, false)
      if #others == 0 then break end
      local to = table.random(others)
      room:delay(500)
      room:doIndicate(player.id, {to.id})
      room:damage { from = player, to = to, damage = 1, skillName = self.name, damageType = fk.FireDamage }
    end
  end,
}
foxwish:addRelatedSkill(emo__nosleep)


Fk:loadTranslationTable{
  ["moesp__foxwish"] = "狐愿",
  ["#moesp__foxwish"] = "夜中行灯",
  ["designer:moesp__foxwish"] = "枫世奇景",
  
  ["emo__guihuo"] = "鬼火",
  [":emo__guihuo"] = "锁定技，每当一名其他角色受到伤害后，令其获得1枚“鬼火”标记：当其使用或打出牌时，移去1枚“鬼火”并随机弃置一张牌。",
  ["@emo__ghostFire"] = "鬼火",

  ["emo__yedeng"] = "夜灯",
  [":emo__yedeng"] = "锁定技，每当牌因〖鬼火〗被弃置后，你获得1枚“夜灯”标记，每获得3枚“夜灯”你摸一张牌，若已有9枚，移除所有“夜灯”，增加一点体力上限并回复一点体力。",
  ["@emo__nightLight"] = "夜灯",

  ["emo__yeming"] = "夜明",
  [":emo__yeming"] = "觉醒技，当你增加体力值上限时，获得技能〖不夜〗，升级〖鬼火〗：令其获得1枚标记改为2枚；升级〖夜灯〗：你获得因〖鬼火〗弃置的牌。",

  ["emo__nosleep"] = "不夜",
  [":emo__nosleep"] = "限定技，出牌阶段，你可以令所有其他角色依次移去所有“鬼火”标记并随机弃置等量的牌，然后你移去所有“夜灯”标记，随机对其他角色分配等量的火焰伤害。",
  ["#emo__nosleep"] = "你可以令所有其他角色移去“鬼火”并弃牌，你移去“夜灯”并对他们造成伤害",

  ["$emo__yeming1"] = "",
  ["$emo__yeming2"] = "",
  ["$emo__nosleep1"] = "",
  ["$emo__nosleep2"] = "",
}

local qinliangyu = General(extension, "moesp__qinliangyu", "moe", 4, 4, General.Female)

local emo__taoni = fk.CreateActiveSkill{
  name = "emo__taoni",
  anim_type = "offensive",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < 1 + player:getMark("emo__taoni-phase")
  end,
  card_filter = Util.FalseFunc,
  card_num = 0,
  prompt = "#emo__taoni",
  target_filter = function(self, to_select, selected)
    if #selected == 0 then
      local player = Fk:currentRoom():getPlayerById(to_select)
      return not player:isKongcheng() and player:getNextAlive() ~= player
    end
  end,
  target_num = 1,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local from = room:getPlayerById(effect.tos[1])
    local cards = table.filter(from:getCardIds(Player.Hand), function (id)
      return Fk:getCardById(id).trueName == "slash"
    end)
    if #cards > 0 then
      room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, self.name, nil, true, player.id)
      room:sendFootnote(cards, {
        type = "##ShowCard",
        from = from.id,
      })
      room:delay(1200)
      local to = from
      for i, id in ipairs(cards) do
        if room:getCardArea(id) == Card.Processing then
          room:doIndicate(to.id, {to:getNextAlive().id})
          to = to:getNextAlive()
          room:delay(i < 10 and 400 or 100)
          room:obtainCard(to, id, true, fk.ReasonJustMove, to.id, self.name)
        end
      end
      room:delay(800)
      room:doIndicate(player.id, {to.id})
      room:damage{
        from = player,
        to = to,
        damage = #cards,
        skillName = self.name,
      }
      if to.dead then return end
    end
    if not player.dead then
      local y = player:usedSkillTimes(self.name, Player.HistoryPhase)
      if room:askForSkillInvoke(player, "emo__taoni", nil, "#emo__taoni-losehp:::"..y) then
        room:addPlayerMark(player, "emo__taoni-phase")
        room:loseHp(player, y, self.name)
      end
    end
  end,
}

--持恒技
emo__taoni.permanent_skill = true

qinliangyu:addSkill(emo__taoni)


local emo__danxin = fk.CreateTriggerSkill{
  name = "emo__danxin",
  events = {fk.Damaged},
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and table.find(player.room.alive_players, function (p)
      return p ~= player and not p:isKongcheng()
    end)
  end,
  on_cost = function (self, event, target, player, data)
    local targets = table.filter(player.room:getOtherPlayers(player, false), function (p) return not p:isKongcheng() end)
    local tos = player.room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#emo__danxin-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    if to:isKongcheng() then return end
    local cards = table.filter(to:getCardIds("h"), function (id)
      return Fk:getCardById(id).trueName == "slash"
    end)
    local a, choice = U.askforChooseCardsAndChoice(player, cards, #cards > 0 and {"#emo__danxin_get", "#emo__danxin_taoni"} or {},
    self.name, "#emo__danxin-watch::"..to.id..":"..#cards, #cards == 0 and {"Cancel"} or {}, 0, 0, to:getCardIds("h"))
    if choice == "#emo__danxin_get" then
      room:obtainCard(player, cards, false, fk.ReasonPrey, player.id, self.name)
      if not to.dead then
        to:drawCards(#cards, self.name)
      end
    elseif choice == "#emo__danxin_taoni" then
      emo__taoni:onUse(room, {from = player.id, tos = {to.id}, cards = {}})
    end
  end,
}

--持恒技
emo__danxin.permanent_skill = true
qinliangyu:addSkill(emo__danxin)

local emo__tianming = fk.CreateActiveSkill{
  name = "emo__tianming",
  anim_type = "offensive",
  frequency = Skill.Limited,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  card_num = 0,
  prompt = "#emo__tianming",
  target_num = 0,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:addPlayerMark(player, "emo__taoni-phase")
    room:setTag("emo__tianming", true)
    -- 偷懒，爽
    room.logic:getCurrentEvent():findParent(GameEvent.Phase):addExitFunc(function()
      for _, p in ipairs(room.alive_players) do
        local cards = table.filter(p:getCardIds("h"), function (id)
          return Fk:getCardById(id).trueName == "slash"
        end)
        if #cards > 0 then
          room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name, nil, true)
        end
      end
      room:shuffleDrawPile()
    end)
    local cards = table.filter(room.draw_pile, function (id)
      return Fk:getCardById(id).trueName == "slash"
    end)
    for i, id in ipairs(cards) do
      if room:getCardArea(id) == Card.DrawPile then
        local tos = room.alive_players
        if #tos == 0 then break end
        room:delay(i < 10 and 250 or 100)
        room:obtainCard(table.random(tos), id, true, fk.ReasonJustMove, player.id, self.name)
      end
    end
  end,
}

emo__tianming.permanent_skill = true
qinliangyu:addSkill(emo__tianming)

Fk:loadTranslationTable{
  ["moesp__qinliangyu"] = "秦良玉",
  ["designer:moesp__qinliangyu"] = "催花高手",
  
  ["emo__taoni"] = "讨逆",
  [":emo__taoni"] = "持恒技，出牌阶段限一次，你可选择一名角色，将该角色所有【杀】置于处理区，按逆时针顺序从其下家开始每名角色获得一张直到没有牌，然后你对最后一名获得牌的角色造成X点伤害（X为以此法分发的牌数）。若没有角色因此死亡，你可以失去Y点体力，令本阶段〖讨逆〗发动技能次数+1（Y为你本阶段已发动次数）。",
  ["#emo__taoni"] = "讨逆：选择一名角色，将其所有【杀】按逆时针分发，你对最后获得的角色造成伤害！",
  ["#emo__taoni-losehp"] = "讨逆：你可以失去 %arg 点体力，令〖讨逆〗发动技能次数+1",

  ["emo__danxin"] = "丹心",
  [":emo__danxin"] = "持恒技，每当你受到伤害后，你可以观看一名其他角色的手牌并选一项：1.获得其所有【杀】，令其摸等量张牌；2.对其发动〖讨逆〗。",
  ["#emo__danxin-choose"] = "丹心：你可观看一名其他角色的手牌，选择获得其的杀或对其“讨逆”",
  ["#emo__danxin-watch"] = "丹心：观看 %dest 的手牌中(其有 %arg 张【杀】)",
  ["#emo__danxin_get"] = "获得其中【杀】",
  ["#emo__danxin_taoni"] = "对其发动“讨逆”",

  ["emo__tianming"] = "天命",
  [":emo__tianming"] = "持恒技，限定技，出牌阶段，你可以将牌堆所有【杀】随机分给每名角色，并令本阶段〖讨逆〗发动技能次数+1，且本阶段结束时，将每名角色的【杀】置入弃牌堆并洗牌。",
  ["#emo__tianming"] = "天命：你可以将牌堆所有【杀】分发，并令〖讨逆〗次数+1",

  ["$emo__tianming1"] = "",
  ["$emo__tianming2"] = "",
}

local yanglan = General(extension, "moesp__yanglan", "moe", 4, 4, General.Female)

local emo__fengwu = fk.CreateTriggerSkill{
  name = "emo__fengwu",
  events = {fk.DamageCaused, fk.DamageInflicted},
  anim_type = "drawCard",
  can_trigger = function(self, event, target, player, data)
    return player == target and player:hasSkill(self) and player:getMark("@@emo__fengwu-turn") == 0
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#emo__fengwu-invoke")
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data.extra_data = data.extra_data or {}
    data.extra_data.emo__fengwuInvoker = data.extra_data.emo__fengwuInvoker or {}
    table.insertIfNeed(data.extra_data.emo__fengwuInvoker, player.id)
    local x = 2 - player.hp
    if x ~= 0 then
      -- 感觉有点危险的内部函数，reason得写nil不然报错
      room:changeHp(player, x, nil, self.name)
      if player.dead then return end
    end
    if x < 0 then
      if not player:hasSkill("ol_ex__kuanggu", true) then
        room:handleAddLoseSkills(player, "ol_ex__kuanggu")
        room.logic:getCurrentEvent():findParent(GameEvent.Turn):addCleaner(function()
          room:handleAddLoseSkills(player, "-ol_ex__kuanggu")
        end)
      end
    else
      room:setPlayerMark(player, "@@emo__fengwu-turn", 1)
      player:drawCards(2, self.name)
    end
  end,
}

local emo__fengwu_delay = fk.CreateTriggerSkill{
  name = "#emo__fengwu_delay",
  priority = 1.01,
  events = {fk.DamageFinished},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and table.contains((data.extra_data or {}).emo__fengwuInvoker or {}, player.id)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local use = room:askForUseCard(player, "slash", "slash", "#emo__fengwu-use", true, {bypass_times = true})
    if use then
      use.extraUse = true
      room:useCard(use)
    end
  end,
}
emo__fengwu:addRelatedSkill(emo__fengwu_delay)

yanglan:addSkill(emo__fengwu)

yanglan:addSkill("mashu")

yanglan:addRelatedSkill("ol_ex__kuanggu")

Fk:loadTranslationTable{
  ["moesp__yanglan"] = "杨兰",
  ["#moesp__yanglan"] = "不死之凤",
  ["designer:moesp__yanglan"] = "怂为真理",
  
  ["emo__fengwu"] = "凤舞",
  [":emo__fengwu"] = "当你造成或受到伤害时，你可以将体力值调整至2点，若你因此减少了体力值，则你本回合获得〖狂骨〗，否则你摸两张牌且本回合〖凤舞〗失效。伤害结算后，你可以使用一张【杀】。",
  ["#emo__fengwu-invoke"] = "凤舞：你可以将体力值调整至2点，伤害结算后你可使用【杀】",
  ["#emo__fengwu_delay"] = "凤舞",
  ["#emo__fengwu-use"] = "凤舞：你可以使用一张【杀】",
  ["@@emo__fengwu-turn"] = "凤舞失效",
}

local aimuheizi = General(extension, "moesp__aimuheizi", "moe", 3, 3, General.Female)


local function yitongColor2Name (player, color)
  if table.contains(player:getTableMark("emo__yitong_used-turn"), color) then return nil end
  for _, id in ipairs(player:getPile("emo__yitongEye")) do
    if Fk:getCardById(id):getColorString() ~= color then
      return Fk:getCardById(id).name
    end
  end
end

local emo__yitong = fk.CreateViewAsSkill{
  name = "emo__yitong",
  card_num = 1,
  target_num = 1,
  derived_piles = "emo__yitongEye",
  prompt = function (self)
    local list = {}
    local used = Self:getTableMark("emo__yitong_used-turn")
    for _, color in ipairs({"red", "black"}) do
      local name = yitongColor2Name(Self, color)
      if name then
        table.insert(list, Fk:translate(color).."：["..Fk:translate(name).."]")
      end
    end
    return "#emo__yitong:::"..table.concat(list, ", ")
  end,
  pattern = ".|.|.|.|.|trick",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and table.contains(Self:getHandlyIds(true), to_select)
    and Fk:getCardById(to_select).color ~= Card.NoColor 
    and not table.contains(Self:getTableMark("emo__yitong_used-turn"), Fk:getCardById(to_select):getColorString())
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return nil end
    local name = yitongColor2Name(Self, Fk:getCardById(cards[1]):getColorString())
    if not name then return nil end
    local c = Fk:cloneCard(name)
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    room:addTableMark(player, "emo__yitong_used-turn", use.card:getColorString())
  end,
  enabled_at_play = function(self, player)
    return #player:getTableMark("emo__yitong_used-turn") < 2 and #player:getPile("emo__yitongEye") > 0
  end,
  enabled_at_response = function (self, player, response)
    if #player:getTableMark("emo__yitong_used-turn") < 2 and #player:getPile("emo__yitongEye") > 0 and
    not response and Fk.currentResponsePattern and not player:isKongcheng() then
      local names = {yitongColor2Name(player, "red"), yitongColor2Name(player, "black")}
      return table.find(names, function (name) 
        return name and Exppattern:Parse(Fk.currentResponsePattern):matchExp(name)
      end)
    end
  end,
}

local emo__yitong_trigger = fk.CreateTriggerSkill{
  name = "#emo__yitong_trigger",
  events = {fk.GameStart},
  anim_type = "drawcard",
  main_skill = emo__yitong,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(emo__yitong)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local red, black = {}, {}
    for _, id in ipairs(room.draw_pile) do
      local c = Fk:getCardById(id)
      if c:isCommonTrick() and not c.is_damage_card then
        if c.color == Card.Red then
          table.insert(red, id)
        elseif c.color == Card.Black then
          table.insert(black, id)
        end
      end
    end
    local cards = {}
    if #red > 0 then table.insert(cards, table.random(red)) end
    if #black > 0 then table.insert(cards, table.random(black)) end
    if #cards == 0 then return end
    player:addToPile("emo__yitongEye", cards, true, self.name)
  end,
}
emo__yitong:addRelatedSkill(emo__yitong_trigger)

aimuheizi:addSkill(emo__yitong)

local emo__nishi = fk.CreateTriggerSkill{
  name = "emo__nishi",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.type == Card.TypeTrick
  end,
  frequency = Skill.Compulsory,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, MarkEnum.AddMaxCardsInTurn)
    local currentEvent = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if not currentEvent then return end
    -- 查询现在是连续第几次使用锦囊牌
    local n = 1
    local events = room.logic.event_recorder[GameEvent.UseCard] or Util.DummyTable
    for i = #events, 1, -1 do
      local e = events[i]
      if e.id < currentEvent.id then
        local use = e.data[1]
        if use.from == player.id then
          if use.card.type == Card.TypeTrick then
            n = n + 1
          else
            break
          end
        end
      end
    end
    if n > 1 then
      room:addPlayerMark(player, "@emo__nishi_basic")
    end
    if n > 2 then
      player:drawCards(1, self.name)
    end
  end,
  
  refresh_events = {fk.CardUsing},
  can_refresh = function (self, event, target, player, data)
   return player == target and data.card.type == Card.TypeBasic and player:getMark("@emo__nishi_basic") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    data.additionalEffect = (data.additionalEffect or 0) + player:getMark("@emo__nishi_basic")
    player.room:setPlayerMark(player, "@emo__nishi_basic", 0) 
  end,

  on_lose = function (self, player, is_death)
    player.room:setPlayerMark(player, "@emo__nishi_basic", 0)
  end,
}
aimuheizi:addSkill(emo__nishi)

Fk:loadTranslationTable{
  ["moesp__aimuheizi"] = "哀目黑子",
  ["#moesp__aimuheizi"] = "逆世之瞳",
  ["designer:moesp__aimuheizi"] = "水天一色",
  
  ["emo__yitong"] = "异瞳",
  [":emo__yitong"] = "游戏开始时，你将牌堆中随机两张颜色不同的非伤害普通锦囊置于你的武将牌上，称为“目”。每回合各限一次，你可以将一张颜色与一张“目”牌相同的手牌当另一张“目”牌使用。",
  ["#emo__yitong"] = "异瞳：你可如此转化手牌：%arg",
  ["#emo__yitong_trigger"] = "异瞳",
  ["emo__yitongEye"] = "目",

  ["emo__nishi"] = "逆世",
  [":emo__nishi"] = "锁定技，每当你使用锦囊牌时，令你本回合手牌上限+1。当你连续使用两张锦囊牌时，令你下一张基本牌额外结算1次。当你连续使用三张锦囊牌时，摸一张牌。",
  ["@emo__nishi_basic"] = "逆世:额外结算",
}


local moesp__altria= General(extension, "moesp__altria", "moe", 3, 5, General.Female)

local emo__tianyou = fk.CreateTriggerSkill{
  name = "emo__tianyou",
  anim_type = "defensive",
  events = {fk.TargetConfirming},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.from ~= player.id and data.card.name ~= "peach" then
      return #table.filter(player:getTableMark(self.name), function(id) return id == data.from end) < 3
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, self.name, data.from)
    AimGroup:cancelTarget(data, player.id)
  end,
}
moesp__altria:addSkill(emo__tianyou)

local emo__fengchui = fk.CreateViewAsSkill{
  name = "emo__fengchui",
  anim_type = "offensive",
  pattern = "slash",
  prompt = "#emo__fengchui",
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local c = Fk:cloneCard("slash")
    c.skillName = self.name
    return c
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  enabled_at_response = function (self, player, response)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
}

local emo__fengchui_trigger = fk.CreateTriggerSkill{
  name = "#emo__fengchui_trigger",
  
  refresh_events = {fk.CardUsing},
  can_refresh = function (self, event, target, player, data)
    if target == player and player:hasSkill(emo__fengchui) then
      local useEvents = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data[1]
        return use.from == player.id
      end, Player.HistoryTurn)
      return useEvents[1] and useEvents[1].data[1] == data
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    data.disresponsiveList = table.map(room.players, Util.IdMapper)
    player:broadcastSkillInvoke("emo__fengchui")
    if (data.card.is_damage_card or data.card:isCommonTrick()) and not table.contains(data.card.skillNames, "emo__fengchui") then
      room:notifySkillInvoked(player, "emo__fengchui")
    end
  end,
}
emo__fengchui:addRelatedSkill(emo__fengchui_trigger)

local emo__fengchui_targetmod = fk.CreateTargetModSkill{
  name = "#emo__fengchui_targetmod",
  bypass_distances = function(self, player, skill, card)
    return card and table.contains(card.skillNames, "emo__fengchui")
  end,
}
emo__fengchui:addRelatedSkill(emo__fengchui_targetmod)

moesp__altria:addSkill(emo__fengchui)

local emo__shengjian = fk.CreateActiveSkill{
  name = "emo__shengjian",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  prompt = "#emo__shengjian",
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    local to = Fk:currentRoom():getPlayerById(to_select)
    return #selected == 0 and to and to ~= Self and (Self.hp > to.hp or Self.maxHp > to.maxHp 
    or Self:getHandcardNum() > to:getHandcardNum() or Self:getMaxCards() > to:getMaxCards())
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    local choices = {}
    if player.hp > to.hp then table.insert(choices, "hp") end
    if player.maxHp > to.maxHp then table.insert(choices, "maxHp") end
    if player:getHandcardNum() > to:getHandcardNum() then table.insert(choices, "handcardNum") end
    if player:getMaxCards() > to:getMaxCards() then table.insert(choices, "maxCards") end
    if #choices == 0 then return end
    room:delay(1500)
    room:damage { from = player, to = to, damage = #choices, skillName = self.name  }
    if player.dead then return end
    if table.contains(choices, "maxCards") then
      room:addPlayerMark(player, MarkEnum.MinusMaxCards)
    end
    if table.contains(choices, "handcardNum") and not player:isKongcheng() then
      if #room:askForDiscard(player, 1, 1, false, self.name, false) == 0 then
        -- 被鸡肋全手牌时强制弃牌
        room:moveCardTo(table.random(player:getCardIds("h")), Card.DiscardPile, nil, fk.ReasonJustMove, self.name)
      end
    end
    if table.contains(choices, "hp") and not player.dead then
      room:changeHp(player, -1, nil, self.name)
    end
    if table.contains(choices, "maxHp") and not player.dead then
      room:changeMaxHp(player, -1)
    end
  end,
}
moesp__altria:addSkill(emo__shengjian)


Fk:loadTranslationTable{
  ["moesp__altria"] = "阿尔托莉雅",
  ["#moesp__altria"] = "遗世独立",
  ["designer:moesp__altria"] = "zwliOrton",
  
  ["emo__tianyou"] = "天佑", -- 阿瓦隆，天佑之岛
  [":emo__tianyou"] = "锁定技，取消你成为每名其他角色对你使用的前三张非【桃】牌的目标。",

  ["emo__fengchui"] = "风锤",
  [":emo__fengchui"] = "每回合限一次，你可以视为使用或打出一张无距离限制的【杀】。你每回合使用的第一张牌不可响应。",
  ["#emo__fengchui"] = "风锤：你可以视为使用或打出一张无距离限制的【杀】",

  ["emo__shengjian"] = "圣剑",
  [":emo__shengjian"] = "出牌阶段限一次，你可以选择一名其他角色，手牌数、手牌上限、体力值、体力上限中每有一项你大于其，你对其造成1点伤害，结算后你的对应数值-1。",
  ["#emo__shengjian"] = "圣剑：选择一名其他角色，比较你与其的手牌数、手牌上限、体力值、体力上限",

  ["$emo__tianyou1"] = "真不错。",
  ["$emo__tianyou2"] = "还差得远！",
  ["$emo__fengchui1"] = "风啊，飞舞吧！",
  ["$emo__fengchui2"] = "风王铁锤！",
  ["$emo__shengjian1"] = "圣剑，解放——",
  ["$emo__shengjian2"] = "誓约胜利之剑——！",
}

local moesp__scoff = General(extension, "moesp__scoff", "moe", 4, 4, General.Female)

local emo__tiancuan = fk.CreateTriggerSkill{
  name = "emo__tiancuan",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseSkipping},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and table.contains({Player.Draw, Player.Play, Player.Discard}, player.phase)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function (p)
      return p:getMark("@emo__tiancuan") == 0
    end)
    if #targets == 0 then return false end
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, 
    "#emo__tiancuan-choose:::"..Util.PhaseStrMapper(player.phase), self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    room:setPlayerMark(to, "@emo__tiancuan", Util.PhaseStrMapper(player.phase))
  end,

  refresh_events = {fk.EventPhaseStart},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("@emo__tiancuan") ~= 0 and player.phase == Player.RoundStart
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, self.name, "negative")
    player:gainAnExtraPhase(Util.PhaseStrMapper(player:getMark("@emo__tiancuan")), true)
    room:setPlayerMark(player, "@emo__tiancuan", 0)
  end,
}

moesp__scoff:addSkill(emo__tiancuan)

local emo__zhiming = fk.CreateTriggerSkill{
  name = "emo__zhiming",
  anim_type = "control",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, data.damage do
      if self.cancel_cost then break end
      if not player:hasSkill(self) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cid = room:getNCards(1)[1]
    room:moveCardTo(cid, Card.Processing, nil, fk.ReasonJustMove, self.name, nil, true, player.id)
    local color = Fk:getCardById(cid).color
    if not player.dead and color ~= Card.NoColor then
      local name = color == Card.Red and "indulgence" or "supply_shortage"
      local targets = table.filter(room.alive_players, function (p)
        return not table.contains(p.sealedSlots, Player.JudgeSlot) and not p:hasDelayedTrick(name)
      end)
      if #targets > 0 then
        local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, 
        "#emo__zhiming-choose:::"..name, self.name, true)
        if #tos > 0 then
          local to = room:getPlayerById(tos[1])
          local trick = Fk:cloneCard(name)
          trick:addSubcard(cid)
          to:addVirtualEquip(trick)
          room:moveCardTo(trick, Player.Judge, to, fk.ReasonJustMove, self.name, nil, true, player.id) 
        end
      end
    end
    room:cleanProcessingArea({cid}, self.name)
  end,
}
moesp__scoff:addSkill(emo__zhiming)


local emo__jiaocuo = fk.CreateTriggerSkill{
  name = "emo__jiaocuo",
  anim_type = "control",
  events = {fk.TurnStart, fk.TurnEnd},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if target:insideExtraTurn() or not player:hasSkill(self) then return false end
    if event == fk.TurnStart then
      return target == player
    elseif target ~= player and not player:isNude() then
      local phases = {2,3,4,5,6,7}
      player.room.logic:getEventsOfScope(GameEvent.Phase, 1, function(e)
        if e.data[1] == target then
          table.removeOne(phases, e.data[2])
        end
        return #phases == 0
      end, Player.HistoryTurn)
      if #phases > 0 then
        self.cost_data = phases
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      local phases = table.map({2,3,4,5,6,7}, Util.PhaseStrMapper)
      local choices = room:askForChoices(player, phases, 1, 3, self.name, "#emo__jiaocuo-choice", true)
      if #choices > 0 then
        self.cost_data = table.map(choices, Util.PhaseStrMapper)
        return true
      end
    else
      local _, dat = room:askForUseActiveSkill(player, "emo__jiaocuo_choose", "#emo__jiaocuo-ask", true, 
      {emo__jiaocuo_phases = table.map(self.cost_data, Util.PhaseStrMapper)})
      if dat then
        self.cost_data = {cards = dat.cards, choice = dat.interaction}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      local phases = self.cost_data
      room:setPlayerMark(player, "emo__jiaocuo_skip-turn", phases)
      player:drawCards(#phases, self.name)
    else
      local phase = self.cost_data.choice ---@type string
      room:throwCard(self.cost_data.cards, self.name, player, player)
      if player.dead then return end
      local turn = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      if turn then
        turn:prependExitFunc(
          function()
            room:sendLog{
              type = "#GainAnPhaseTurn",
              from = player.id,
              arg = phase,
            }

            local current = room.current
            room.current = player

            player.tag["_extra_turn_count"] = player.tag["_extra_turn_count"] or {}
            local ex_tag = player.tag["_extra_turn_count"]
            table.insert(ex_tag, "emoJiaocuoTurn" .. phase)

            GameEvent.Turn:create(player):exec()

            table.remove(ex_tag)

            room.current = current
          end
        )
      end

    end
  end,
}

local emo__jiaocuo_delay = fk.CreateTriggerSkill{
  name = "#emo__jiaocuo_delay",
  mute = true,
  priority = 2, -- 需要比贵相等修改阶段的技能优先
  events = {fk.EventPhaseChanging},
  can_trigger = function(self, event, target, player, data)
    return target == player and table.contains(player:getTableMark("emo__jiaocuo_skip-turn"), data.to)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:removeTableMark(player, "emo__jiaocuo_skip-turn", data.to)
    return true
  end,

  refresh_events = {fk.EventPhaseChanging},
  can_refresh = function(self, event, target, player, data)
    local extraTurnInfo = player.tag["_extra_turn_count"]

    return
      target == player and 
      type(extraTurnInfo) == "table" and
      #extraTurnInfo > 0 and
      type(extraTurnInfo[#extraTurnInfo]) == "string" and
      extraTurnInfo[#extraTurnInfo]:startsWith("emoJiaocuoTurn") and
      data.to == Player.RoundStart
  end,
  on_refresh = function(self, event, target, player, data)
    local excludePhases = { 2,3,4,5,6,7 }
    local extraTurnInfo = player.tag["_extra_turn_count"]
    local phase = string.sub(extraTurnInfo[#extraTurnInfo], 15)
    table.removeOne(excludePhases, Util.PhaseStrMapper(phase))
    for _, p in ipairs(excludePhases) do
      table.removeOne(player.phases, p)
    end
  end,
}
emo__jiaocuo:addRelatedSkill(emo__jiaocuo_delay)

moesp__scoff:addSkill(emo__jiaocuo)

local emo__jiaocuo_choose = fk.CreateActiveSkill{
  name = "emo__jiaocuo_choose",
  card_num = 1,
  target_num = 0,
  interaction = function(self)
    return UI.ComboBox { choices = self.emo__jiaocuo_phases }
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and not Self:prohibitDiscard(Fk:getCardById(to_select))
  end,
}
Fk:addSkill(emo__jiaocuo_choose)

Fk:loadTranslationTable{
  ["moesp__scoff"] = "嗤",
  ["#moesp__scoff"] = "戏谑命运",
  ["designer:moesp__scoff"] = "手枪顿豆腐",
  
  ["emo__tiancuan"] = "天篡",
  [":emo__tiancuan"] = "当你跳过摸牌、出牌或弃牌阶段时，你可以令一名其他角色于下回合开始时执行一个相同阶段（直到其回合开始不能对其发动〖天篡〗）。",
  ["@emo__tiancuan"] = "天篡",
  ["#emo__tiancuan-choose"] = "天篡：选择一名其他角色，令其下回合开始时执行 %arg",

  ["emo__zhiming"] = "织命",
  [":emo__zhiming"] = "每当你受到1点伤害后，你可以亮出牌堆顶牌，然后可以按之颜色：红色当【乐不思蜀】，黑色当【兵粮寸断】置于一名角色的判定区。",
  ["#emo__zhiming-choose"] = "织命：你可以将此牌当【%arg】置于一名角色判定区",

  ["emo__jiaocuo"] = "交错",
  [":emo__jiaocuo"] = "你的额定回合开始时，你可以跳过至多三个阶段，并摸等量张牌。"..
  "<br>其他角色的额定回合结束时，你可以弃置一张牌，执行一个其本回合未执行的阶段。",
  ["#emo__jiaocuo-choice"] = "交错：你可以跳过至多三个阶段，并摸等量张牌",
  ["#emo__jiaocuo_delay"] = "交错",
  ["#emo__jiaocuo-ask"] = "交错：你可以弃置一张牌，执行一个其本回合未执行的阶段",
  ["emo__jiaocuo_choose"] = "交错",
  ["#GainAnPhaseTurn"] = "%from 执行一个仅有 %arg 的额外回合",
}


local moesp__caoxian = General(extension, "moesp__caoxian", "moe", 3, 3, General.Female)
moesp__caoxian.subkingdom = "wei"

local emo__lingxi = fk.CreateTriggerSkill{
  name = "emo__lingxi",
  events = {fk.RoundStart, fk.TurnStart, fk.TargetConfirmed, fk.AfterCardsMove},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.RoundStart then
      return player:usedSkillTimes(self.name, Player.HistoryGame) == 0 and table.find(player.room.alive_players, function (p)
        return p ~= player end)
    elseif event == fk.TurnStart then
      return not target:isKongcheng() and target.id == player:getMark(self.name)
    else
      local cards = {}
      if event == fk.TargetConfirmed then
        if target == player and data.from == player:getMark(self.name) then
          cards = Card:getIdList(data.card)
        end
      else
        for _, move in ipairs(data) do
          if move.from == player:getMark(self.name) and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Player.Hand or info.fromArea == Player.Equip then
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end
      end
      local mark = player:getTableMark("emo__lingxi_gain")
      cards = table.filter(cards, function (id)
        return not table.contains(mark, id) and
        (player.room:getCardArea(id) == Card.Processing or player.room:getCardArea(id) == Card.DiscardPile)
      end)
      if #cards > 0 then
        self.cost_data = cards
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local tos = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper), 1, 1, "#emo__lingxi-choose", self.name, false)
      local to = room:getPlayerById(tos[1])
      room:addPlayerMark(to, "@@emo__lingxi_other")
      room:setPlayerMark(player, self.name, to.id)
      room:setPlayerMark(player, "@emo__lingxi", to.general)
    elseif event == fk.TurnStart then
      room:delay(500)
      room:obtainCard(player, target:getCardIds("h"), false, fk.ReasonGive, target.id, self.name)
    else
      local cards = self.cost_data
      local mark = player:getTableMark("emo__lingxi_gain")
      table.insertTable(mark, cards)
      room:setPlayerMark(player, "emo__lingxi_gain", mark)
      room:obtainCard(player, cards, true, fk.ReasonJustMove, player.id, self.name)
    end
  end,

  refresh_events = {fk.EventLoseSkill, fk.BuryVictim},
  can_refresh = function(self, event, target, player, data)
    if target == player and player:getMark(self.name) ~= 0 then
      if event == fk.EventLoseSkill then
        return data == self
      else
        return player:hasSkill(self, true, true) and player.rest == 0
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(player:getMark(self.name))
    if to then
      room:removePlayerMark(to, "@@emo__lingxi_other")
    end
    room:setPlayerMark(player, self.name, 0)
    room:setPlayerMark(player, "@emo__lingxi", 0)
  end,
}
moesp__caoxian:addSkill(emo__lingxi)

local emo__zhifou = fk.CreateTriggerSkill{
  name = "emo__zhifou",
  anim_type = "support",
  frequency = Skill.Compulsory,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    -- 可能死循环，限制一下发动次数
    if player:hasSkill(self) and not player:isKongcheng() and player:usedSkillTimes(self.name, Player.HistoryTurn) < 99 then
      for _, move in ipairs(data) do
        if move.to == player.id and move.toArea == Player.Hand and move.from and move.from ~= player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Player.Hand or info.fromArea == Player.Equip then
              return true
            end
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local numMap = {}
    for _, move in ipairs(data) do
      if move.to == player.id and move.toArea == Player.Hand and move.from and move.from ~= player.id then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Player.Hand or info.fromArea == Player.Equip then
            numMap[move.from] = (numMap[move.from] or 0) + 1
          end
        end
      end
    end
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if player:isKongcheng() or not player:hasSkill(self) then return end
      if not p.dead and numMap[p.id] then
        self:doCost(event, p, player, numMap[p.id])
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    self.cost_data = {tos = {target.id}}
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local first
    if not table.contains(player:getTableMark("emo__zhifou_tar-round"), target.id) then
      room:addTableMark(player, "emo__zhifou_tar-round", target.id)
      first = true
    end
    local suits = {}
    for _, id in ipairs(player:getCardIds("h")) do
      table.insertIfNeed(suits, Fk:getCardById(id).suit)
    end
    local x = #suits
    local choice = room:askForChoice(player, {"#emo__zhifou_give::"..target.id..":"..x..":"..data, "#emo__zhifou_draw::"..target.id..":"..x}, self.name)
    if choice:startsWith("#emo__zhifou_give") then
      player:drawCards(x, self.name)
      if not target.dead and not player:isKongcheng() then
        local cards = player:getCardIds("h")
        if #cards > data then
          cards = room:askForCard(player, data, data, false, self.name, false, ".", "#emo__zhifou-give::"..target.id..":"..data)
        end
        room:obtainCard(target, cards, false, fk.ReasonGive, player.id, self.name)
      end
    else
      target:drawCards(x, self.name)
    end
    if first and not target.dead and not player.dead then
      choice = room:askForChoice(player, {"#emo__zhifou_recover::"..target.id, "#emo__zhifou_losehp::"..target.id}, self.name)
      if choice:startsWith("#emo__zhifou_recover") then
        room:recover {num = 1, skillName = self.name, who = target, recoverBy = player}
      else
        room:loseHp(target, 1, self.name)
      end
    end
  end,
}
moesp__caoxian:addSkill(emo__zhifou)


Fk:loadTranslationTable{
  ["moesp__caoxian"] = "萌曹宪",
  ["#moesp__caoxian"] = "蝶步韶华",
  ["designer:moesp__caoxian"] = "曹宪咬我",
  
  ["emo__lingxi"] = "灵犀",
  [":emo__lingxi"] = "锁定技，每轮开始时，若你未发动此技能，你令一名其他角色获得“灵”标记。其回合开始时你获得其所有手牌。其弃置牌或对你使用牌时你获得之（每张牌限一次）。",
  ["@emo__lingxi"] = "灵犀",
  ["@@emo__lingxi_other"] = "灵",
  ["#emo__lingxi-choose"] = "灵犀：令一名其他角色获得“灵”标记，其每回合开始时你获得其手牌",

  ["emo__zhifou"] = "知否",
  [":emo__zhifou"] = "锁定技，每当你获得其他角色的牌时，你须选一项："..
  "<br>1.摸X张牌，交给其等量于获得牌数张手牌；2.令其摸X张牌。（X为你手牌中花色数）"..
  "<br>若为本轮第一次对其发动，你令其回复或失去一点体力。",
  ["#emo__zhifou_give"] = "摸 %arg 张牌，交给 %dest %arg2 张手牌",
  ["#emo__zhifou_draw"] = "令 %dest 摸 %arg 张牌",
  ["#emo__zhifou-give"] = "知否：请交给 %dest %arg 张手牌",
  ["#emo__zhifou_recover"] = "令 %dest 回复1点体力",
  ["#emo__zhifou_losehp"] = "令 %dest 失去1点体力",

  ["$emo__lingxi1"] = "此心相印，非山河可阻，兵戈可断。",
  ["$emo__lingxi2"] = "角中有白纹，灵如线，君心我心系两端。",
  ["$emo__zhifou1"] = "君知春风带暖意，可知丝丝皆妾语？",
  ["$emo__zhifou2"] = "愿将满腹相思语，化为春风拂君面。",
  ["~moesp__caoxian"] = "君薄情，父寡义，妾将何为？",
}

local moesp__atri = General(extension, "moesp__atri", "moe", 4, 4, General.Female)

local emo__renzhu = fk.CreateTriggerSkill{
  name = "emo__renzhu",
  anim_type = "support",
  events = {fk.GameStart, fk.Damaged, fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.GameStart then
        return table.find(player.room.alive_players, function (p) return p ~= player end)
        and player:getMark("emo__renzhu_tar") == 0
      elseif event == fk.Damaged then
        if not target.dead and target.id == player:getMark("emo__renzhu_tar") and data.from and not data.from.dead then
          local slash = Fk:cloneCard("slash")
          slash.skillName = self.name
          return not player:prohibitUse(slash) and not player:isProhibited(data.from, slash)
        end
      else
        return target == player and player.room:getPlayerById(player:getMark("emo__renzhu_tar")) ~= nil
        and not player.room:getPlayerById(player:getMark("emo__renzhu_tar")).dead
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.GameStart then
      local tos = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), Util.IdMapper),
      1, 1, "#emo__renzhu-choose", self.name, false)
      if #tos > 0 then
        self.cost_data = {tos = tos}
        return true
      end
    elseif event == fk.Damaged then
      if player.room:askForSkillInvoke(player, self.name, nil, "#emo__renzhu-slash:"..data.from.id) then
        self.cost_data = {tos = {data.from.id}}
        return true
      end
    else
      local master = player.room:getPlayerById(player:getMark("emo__renzhu_tar"))
      if player.room:askForSkillInvoke(master, self.name, nil, "#emo__renzhu-transfer:"..player.id) then
        self.cost_data = {tos = {master.id}}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local to = room:getPlayerById(self.cost_data.tos[1])
      room:addPlayerMark(to, "@@emo__master")
      room:setPlayerMark(player, "emo__renzhu_tar", to.id)
      room:setPlayerMark(player, "@emo__renzhu", to.general)
    elseif event == fk.Damaged then
      local slash = Fk:cloneCard("slash")
      slash.skillName = self.name
      room:useCard{ from = player.id, tos = {{data.from.id}}, card = slash, extraUse = true, additionalDamage = 1 }
    else
      room:damage { from = data.from, to = room:getPlayerById(player:getMark("emo__renzhu_tar")), damage = 1,
      skillName = self.name, damageType = fk.ThunderDamage }
      return true
    end
  end,

  refresh_events = {fk.EventLoseSkill, fk.BuryVictim},
  can_refresh = function (self, event, target, player, data)
    if event == fk.EventLoseSkill then
      return target == player and data == self and player:getMark("emo__renzhu_tar") ~= 0
    else
      return target.id == player:getMark("emo__renzhu_tar") and target.rest == 0
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local to = target
    if event == fk.EventLoseSkill then
      to = room:getPlayerById(player:getMark("emo__renzhu_tar"))
      room:setPlayerMark(player, "emo__renzhu_tar", 0)
      room:setPlayerMark(player, "@emo__renzhu", 0)
    else
      room:setPlayerMark(player, "emo__renzhu_tar", 0)
      room:setPlayerMark(player, "@emo__renzhu", 0)
    end
    if table.every(room.alive_players, function (p) return p:getMark("emo__renzhu_tar") ~= to.id end) then
      room:setPlayerMark(to, "@@emo__master", 0)
    end
  end,
}
moesp__atri:addSkill(emo__renzhu)


local emo__shixie = fk.CreateTriggerSkill{
  name = "emo__shixie",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.card and table.contains(data.card.skillNames, "emo__renzhu") 
    and not data.to.dead and player:getMark("emo__renzhu_tar") ~= 0 
    and not player.room:getPlayerById(player:getMark("emo__renzhu_tar")).dead then
      local cardId = table.find(player.room.draw_pile, function (id)
        return (Fk:getCardById(id).sub_type == Card.SubtypeOffensiveRide or Fk:getCardById(id).sub_type == Card.SubtypeDefensiveRide)
        and player:canMoveCardIntoEquip(id, true)
      end)
      if cardId then
        self.cost_data = {cards = {cardId}}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local master = room:getPlayerById(player:getMark("emo__renzhu_tar"))
    local choices = {"#emo__shixie_horse::"..player.id, "#emo__shixie_damage::"..player.id..":"..data.to.hp}
    local choice = room:askForChoice(master, choices, self.name, "#emo__shixie-choice:"..data.to.id..":"..player.id)
    if choice == choices[1] then
      room:moveCardIntoEquip(player, self.cost_data.cards[1], self.name, true, player)
    else
      data.damage = data.damage + data.to.hp
      room:loseHp(player, 1, self.name)
    end
  end,
}
moesp__atri:addSkill(emo__shixie)


local emo__tongxin = fk.CreateTriggerSkill{
  name = "emo__tongxin",
  anim_type = "support",
  frequency = Skill.Limited,
  events = {fk.BeforeGameOverJudge},
  can_trigger = function(self, event, target, player, data)
    return target.id == player:getMark("emo__renzhu_tar") and player:hasSkill(self) and target.rest == 0
    and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name, nil, "#emo__tongxin-invoke:"..target.id) then
      self.cost_data = {tos = {target.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    target._splayer:setDied(false)
    room:setPlayerRest(target, 1)
    -- 让自己死亡并进入休整，然后结束自己的回合
    room:setPlayerRest(player, 1)
    player.dead = true
    table.removeOne(room.alive_players, player) -- 这个竟然能同步给客户端，神奇
    room:broadcastProperty(player, "dead")
    if player.phase ~= Player.NotActive then
      room:endTurn()
    end
  end,
}
emo__tongxin.permanent_skill = true

moesp__atri:addSkill(emo__tongxin)


Fk:loadTranslationTable{
  ["moesp__atri"] = "亚托莉",
  ["#moesp__atri"] = "萝卜子",
  ["designer:moesp__atri"] = "Atri",
  
  ["emo__renzhu"] = "认主",
  [":emo__renzhu"] = "游戏开始时，你令一名其他角色获得“主”标记：每当该角色受到伤害后，你可以视为对伤害来源使用一张伤害基数+1的【杀】；当你受到伤害时，该角色可以防止此伤害，然后受到伤害来源的一点雷电伤害。",
  ["@@emo__master"] = "主",
  ["@emo__renzhu"] = "认主",
  ["#emo__renzhu-choose"] = "认主：选择一名其他角色作为你的“主”：其受到伤害后你可视为对来源使用【杀】",
  ["#emo__renzhu-slash"] = "认主：你可以视为对 %src 使用【杀】",
  ["#emo__renzhu-transfer"] = "认主：你可以防止 %src 即将受到的伤害，然后你受到1点雷电伤害",

  ["emo__shixie"] = "拾鞋",
  [":emo__shixie"] = "锁定技，当你因〖认主〗使用的【杀】造成伤害时，若牌堆中有可以置入你装备区的坐骑牌（可替换），你令有“主”标记的角色选一项：1.将此坐骑牌置入你的装备区；2.你失去一点体力，令此伤害值+X（X为受伤角色当前体力值）。",
  ["#emo__shixie_horse"] = "将一张坐骑置入 %dest 的装备区",
  ["#emo__shixie_damage"] = "%dest 失去1点体力，此伤害+%arg",
  ["#emo__shixie-choice"] = "拾鞋：%src 即将受到 %dest 造成的伤害，请选一项",

  ["emo__tongxin"] = "同心",
  [":emo__tongxin"] = "持恒技，限定技，当有“主”标记的角色死亡时，你可以令其进入一轮修整，然后你进入一轮修整。",
  ["#emo__tongxin-invoke"] = "同心：你可以令你与 %src 一起修整一轮！",

  ["$emo__renzhu1"] = "我叫亚托莉！",
  ["$emo__renzhu2"] = "我是高性能的嘛",
  ["$emo__shixie1"] = "我是个怪物……",
  ["$emo__shixie2"] = "我……派上用场了吗？",
  ["$emo__tongxin1"] = "夏生先生……我喜欢你",
  ["$emo__tongxin2"] = "欢迎回来，夏生先生",
}


-- 寻找开启牌堆的兵临城下（优先级：汗青、国际服、手杀）
---@return string?
local getBinglinCard = function ()
  for _, name in ipairs({"hanqing__enemy_at_the_gates", "enemy_at_the_gates", "mobile__enemy_at_the_gates"}) do
    if Fk.all_card_types[name] ~= nil then
      return name
    end
  end
  return nil
end

local moesp__kawakaze = General(extension, "moesp__kawakaze", "moe", 4, 4, General.Female)

local emo__nixi = fk.CreateActiveSkill{
  name = "emo__nixi",
  anim_type = "offensive",
  card_num = 0,
  target_num = 0,
  prompt = "#emo__nixi",
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return not player:isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = table.reverse(player:getCardIds("h"))
    local top = room:getNCards(4)

    room:moveCards({
      from = player.id,
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonExchange,
      proposer = player.id,
      skillName = self.name,
      moveVisible = false,
      visiblePlayers = player.id,
    },{
      ids = top,
      toArea = Card.Processing,
      moveReason = fk.ReasonExchange,
      proposer = player.id,
      skillName = self.name,
      moveVisible = false,
      --visiblePlayers = player.id,
    })

    cards = table.filter(cards, function (id) return room:getCardArea(id) == Card.Processing end)
    top = table.filter(top, function (id) return room:getCardArea(id) == Card.Processing end)
    if not player.dead and #cards > 1 then
      cards = room:askForGuanxing(player, cards, nil, {0,0}, self.name, true).top
    end
    
    local moveInfos = {}
    if #cards > 0 then
      table.insert(moveInfos, {
        ids = cards,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonExchange,
        proposer = player.id,
        skillName = self.name,
        moveVisible = false,
        visiblePlayers = player.id,
      })
    end

    if not player.dead then
      table.insert(moveInfos, {
        ids = top,
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonExchange,
        proposer = player.id,
        skillName = self.name,
        moveVisible = false,
        visiblePlayers = player.id,
      })
    end
    if #moveInfos > 0 then
      room:moveCards(table.unpack(moveInfos))
    end
    room:cleanProcessingArea(table.connect(cards, top), self.name)

    if player.dead then return end
    local cardname = getBinglinCard()
    local use
    if cardname then
      use = U.askForUseVirtualCard(room, player, "enemy_at_the_gates", nil, self.name, nil, false, true, true, true)
    end
    -- cannot use normal damageDealt to judge whether the damage is caused
    if not (use and use.extra_data and use.extra_data.enemy_at_the_gates_damageDealt) then
      room:invalidateSkill(player, self.name, "-phase")
    end
  end,
}

local emo__nixi_trigger = fk.CreateTriggerSkill{
  name = "#emo__nixi_trigger",
  
  refresh_events = {fk.Damage},
  can_refresh = function (self, event, target, player, data)
    return target == player and data.card and data.card.trueName == "slash"
  end,
  on_refresh = function (self, event, target, player, data)
    local e = player.room.logic:getCurrentEvent().parent
    while e do
      if e.event == GameEvent.UseCard then
        local use = e.data[1]
        if use.card.trueName == "enemy_at_the_gates" then
          use.extra_data = use.extra_data or {}
          use.extra_data.enemy_at_the_gates_damageDealt = true
          return
        end
      end
      e = e.parent
    end
  end,
}
emo__nixi:addRelatedSkill(emo__nixi_trigger)

moesp__kawakaze:addSkill(emo__nixi)

Fk:loadTranslationTable{
  ["moesp__kawakaze"] = "江风",
  ["designer:moesp__kawakaze"] = "狸猫换叶子",
  
  ["emo__nixi"] = "逆袭", -- 塔萨法隆格逆袭
  [":emo__nixi"] = "出牌阶段，你可用所有手牌与牌堆顶的四张牌交换并视为使用一张【兵临城下】。若没有造成伤害，此技能无效至本阶段结束。",
  ["#emo__nixi"] = "逆袭：用所有手牌与牌堆顶的四张牌交换，视为使用一张【兵临城下】",
}


-- 萌包三联动征集优胜投稿
local moesp__xiajie = General(extension, "moesp__xiajie", "moe", 3, 3, General.Female)

local emo__yuanji = fk.CreateTriggerSkill{
  name = "emo__yuanji",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.Damage, fk.Damaged},
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(3)
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, self.name, nil, true, player.id)
    cards = table.filter(cards, function (id) return room:getCardArea(id) == Card.Processing end)
    local get = {}
    for _, id in ipairs(cards) do
      local c = Fk:getCardById(id)
      if not c.is_damage_card then
        room:setCardEmotion(id, "judgegood")
        table.insert(get, id)
      end
    end
    room:delay(700)
    if #get > 0 then
      if player:isAlive() then
        room:obtainCard(player, get, true, fk.ReasonJustMove, player.id, self.name)
      else
        room:cleanProcessingArea(get, self.name)
      end
    end
    cards = table.filter(cards, function (id) return room:getCardArea(id) == Card.Processing end)
    if #cards > 0 then
      room:moveCards{
        ids = cards,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonJustMove,
        moveVisible = false,
        skillName = self.name,
        drawPilePosition = -1,
      }
    end
  end,
}
moesp__xiajie:addSkill(emo__yuanji)


local emo__dangjing = fk.CreateViewAsSkill{
  name = "emo__dangjing",
  anim_type = "offensive",
  prompt = "#emo__dangjing",
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local name = getBinglinCard()
    if not name then return nil end
    local c = Fk:cloneCard(name)
    c.skillName = self.name
    return c
  end,
  after_use = function(self, player, use)
    local room = player.room
    if player.dead then return end
    if not (use.extra_data and use.extra_data.emo__dangjing_damageDealt) then
      local targets = TargetGroup:getRealTargets(use.tos)
      room:sortPlayersByAction(targets)
      for _, pid in ipairs(targets) do
        local from = room:getPlayerById(pid)
        if not from.dead and not player.dead then
          room:useVirtualCard(use.card.name, nil, from, player, self.name)
        end
      end
    end
  end,
  enabled_at_play = function(self, player)
    return player.phase == Player.Play and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  enabled_at_response = Util.FalseFunc,
}

local emo__dangjing_trigger = fk.CreateTriggerSkill{
  name = "#emo__dangjing_trigger",

  refresh_events = {fk.Damage},
  can_refresh = function (self, event, target, player, data)
    return target == player and data.card and data.card.trueName == "slash"
  end,
  on_refresh = function (self, event, target, player, data)
    local e = player.room.logic:getCurrentEvent().parent
    while e do
      if e.event == GameEvent.UseCard then
        local use = e.data[1]
        if use.card.trueName == "enemy_at_the_gates" and table.contains(use.card.skillNames, "emo__dangjing") then
          use.extra_data = use.extra_data or {}
          use.extra_data.emo__dangjing_damageDealt = true
          return
        end
      end
      e = e.parent
    end
  end,
}
emo__dangjing:addRelatedSkill(emo__dangjing_trigger)

moesp__xiajie:addSkill(emo__dangjing)

local emo__haomo = fk.CreateTriggerSkill{
  name = "emo__haomo",
  anim_type = "offensive",
  frequency = Skill.Limited,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish
    and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    while not player.dead do
      local cid = room:getNCards(1)[1]
      room:moveCardTo(cid, Card.Processing, nil, fk.ReasonJustMove, self.name, nil, true, player.id)
      table.insert(cards, cid)
      local c = Fk:getCardById(cid)
      if c.is_damage_card or c.type == Card.TypeBasic then
        room:setCardEmotion(cid, "judgegood")
        room:delay(#cards < 6 and 700 or 100)
      else
        room:setCardEmotion(cid, "judgebad")
        break
      end
    end
    cards = table.filter(cards, function (id) return room:getCardArea(id) == Card.Processing end)
    if #cards > 0 and player:isAlive() then
      room:delay(500)
      room:obtainCard(player, cards, true, fk.ReasonJustMove, player.id, self.name)
    end
    room:cleanProcessingArea(cards, self.name)
  end,

  refresh_events = {fk.AfterDrawPileShuffle},
  can_refresh = function (self, event, target, player, data)
    return player:usedSkillTimes(self.name, Player.HistoryGame) > 0
  end,
  on_refresh = function (self, event, target, player, data)
    player:setSkillUseHistory(self.name, 0, Player.HistoryGame)
  end,
}
moesp__xiajie:addSkill(emo__haomo)

Fk:loadTranslationTable{
  ["moesp__xiajie"] = "夏桀",
  ["#moesp__xiajie"] = "倒行逆施",
  ["designer:moesp__xiajie"] = "胖即是胖",
  ["cv:moesp__xiajie"] = "桃妮儿",
  
  ["emo__yuanji"] = "怨积",
  [":emo__yuanji"] = "锁定技，当你造成或受到伤害后，你亮出牌堆顶的三张牌并获得非伤害类牌，剩余牌置于牌堆底。",

  ["emo__dangjing"] = "荡境",
  [":emo__dangjing"] = "出牌阶段限一次，你可以视为使用一张【兵临城下】，然后若你未因此造成伤害，目标角色视为对你使用一张【兵临城下】。",
  ["#emo__dangjing"] = "荡境：视为使用【兵临城下】，若未造成伤害，目标视为对你使用【兵临城下】",

  ["emo__haomo"] = "浩末",
  [":emo__haomo"] = "限定技，结束阶段，你可以重复亮出牌堆顶的牌直到未亮出基本牌或伤害类牌，然后获得这些牌；当牌堆洗牌后，此技能视为未发动过。",

  ["$emo__yuanji1"] = "下民无知，奉予不恭，合当孥戮。",
  ["$emo__yuanji2"] = "时日曷丧？哈哈，咸拟一万年！",
  ["$emo__dangjing1"] = "不聘千里，不奋六翮，盖予生而有之。",
  ["$emo__dangjing2"] = "九川刮泥而成岛屿，不刮民何以成家国。",
  ["$emo__haomo1"] = "仰天光兮自列，招上帝兮我察！予有何罪？",
  ["$emo__haomo2"] = "龙登玄云，神栖昆仑；予有龙肝，欲禽神人佐之。",
  ["~moesp__xiajie"] = "上天弗恤！日月不时！大命何归？",
  -- 胜利：平水土、别九州，天赐玄珪，岂可混同凡类？
}










--[[

local moesp__xxxx = General(extension, "moesp__xxxx", "moe", 4, 4, General.Female)

Fk:loadTranslationTable{
  ["moesp__xxxx"] = "",
  ["designer:moesp__xxxx"] = "",
  
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",

  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
}

--]]











return extension

